0

I have a list of objects and I need to loop thru the list and change a value from true to false. Shouldn't a simple for loop do the trick? Am I missing something?

var list = [
  { color: 'blue', 'taste': 'sour', 'available': true },
  { color: 'yellow', 'taste': 'bitter', 'available': false },
  { color: 'red', 'taste': 'sweet', 'available': false },
  { color: 'green', 'taste': 'umami', 'available': false }
]

for(var i = 0; i < list.length; i++){
  if(list[i].available === true){
    list[i].available === false;
  }
}

When I return the list though it's giving me the list as it was first captured. Am I using the wrong loop or is it something else?

3
  • 1
    Purely a style thing, but why is color an unquoted key when all of your other keys are quoted? Inconsistencies like that have a way of hiding errors... Commented May 31, 2016 at 18:52
  • 3
    list[i].available === false is performing a comparison. You need to do an assignment: list[i].available = false Commented May 31, 2016 at 18:52
  • Oh that's just a typo. I was just trying to quickly mock up a bunch of objects in an array. Commented May 31, 2016 at 18:54

2 Answers 2

3
var list = [
  { color: 'blue', 'taste': 'sour', 'available': true },
  { color: 'yellow', 'taste': 'bitter', 'available': false },
  { color: 'red', 'taste': 'sweet', 'available': false },
  { color: 'green', 'taste': 'umami', 'available': false }
]

for(var i = 0; i < list.length; i++){
  if(list[i].available === true){
   // you were not modifying here, just comparing
    list[i].available = false;
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Mostly nitpicking here but the comparison to === true is not necessary.
that about solves that question, @Alex Marple i think that was your issue, please mark accepted answer if your good. no point necessary here for anyone (except rishab dev) since the user made a mistake, this is how answers get bloated. Or im going to guess the first 3 upvotes are rishabh dev? :-)
Ahhh yes. I knew I was missing something troublingly simple. Thank you. Fixed. I will accept this in 10 minutes when it lets me.
omg now someone downvoted you. dude what the heck is happening to the stack crowd here? lol, thanks for recanting that one..
1

You are doing comparision using list[i].available === false.You need to do assign false into list[i].available.So, try this list[i].available = false.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.