Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Local Storage Project

Marcus Schumacher
Marcus Schumacher
16,616 Points

Trying to do something simple with Local Storage

I have an Item in local storage called storedItems

storedItems: "[{"name": "Phil", "going": "null"},{another object},{another}]"

It's an array of objects, meant to organize names and the status of weather or not they are attending an event. This is a challenge from the treehouse course dom scripting by example.

I'm trying to access the going property of a certain object in storedItems, and change it to the boolean I want. What am I doing wrong?

Here is what getStoredListItems() does :

function getStoredListItems() {
  var storedItems = localStorage.getItem('storedItems'); 
  if(storedItems) {
    return JSON.parse(storedItems);
  }
  else {
   return []; 
  }
}

This is where the problem comes in

getStoredListItems().forEach(function(obj){

          if(obj.name == liText) { // liText ins't shown here, but this if block fires correctly
             function updateGoing(obj, bool) {
                  var stringyBool = JSON.stringify(bool);
                  localStorage.setItem(obj.going, stringyBool);
              }
            updateGoing(obj, true); // this doesn't work. obj.going stays null, it's default value.
             // yes, i've tried refreshing the page
          }

        });

1 Answer

Steven Parker
Steven Parker
231,275 Points

Did you look at what's being put into the local storage?

[object Storage] {
  null: "true",
  true: "true"
}

Try using "console.log" to show the arguments you are passing to the local storage function. I'll bet it's not what you thought you were giving it.