Alex Marple Alex Marple - 6 months ago 11
Javascript Question

Changing value in list of objects

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?

Answer
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;
  }
}