Reputation: 3048
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?
Upvotes: 0
Views: 49
Reputation: 27192
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
.
Upvotes: 1
Reputation: 1743
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;
}
}
Upvotes: 3