//loping for objects
const users = [
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false},
]
function setUsersToPremium(users) {
// users is an array of user objects.
// each user object has the property 'isPremium'
// set each user's isPremium property to true
// return the users array
for (let key in users) {
users['isPremium'] = true;
}
return users;
}
setUsersToPremium(users);
I am true to figure out how to loop through an array of objects and change their value from false to true. My result with this code is
[true, true, true, true, true ]
but what I want to do is change each
[isPremium: true]
I'm wondering what I'm doing that is keeping me from accessing this value.
-
1Also see Why is using "for...in" with array iteration a bad idea?JJJ– JJJ2018年03月18日 07:51:29 +00:00Commented Mar 18, 2018 at 7:51
-
users[key].isPremium = truepatrick– patrick2018年03月18日 08:44:27 +00:00Commented Mar 18, 2018 at 8:44
6 Answers 6
You have to set as users[key].isPremium = true;
//loping for objects
const users = [
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false},
]
function setUsersToPremium(users) {
for (let key in users) {
users[key].isPremium = true;
}
//return users; <-- No need to return. You are changing the user variable directly with these code.
}
setUsersToPremium(users);
console.log(users);
The ideal way to do this is to use forEach instead of for/in
const users = [{isPremium: false},{isPremium: false},{isPremium: false},{isPremium: false},{isPremium: false},];
function setUsersToPremium(users) {
users.forEach(o => o.isPremium = true);
}
setUsersToPremium(users);
console.log(users);
If you don't want to affect the original array, you can clone the object using Object.assign
const users = [{isPremium: false},{isPremium: false},{isPremium: false},{isPremium: false},{isPremium: false},];
function setUsersToPremium(users) {
return users.map(o => {
let x = Object.assign({}, o);
x.isPremium = true;
return x;
});
}
var newUsers = setUsersToPremium(users);
console.log(newUsers);
Comments
You are using for..in loop for an array so you'll be getting indexes of that array (1,2,3...)
To fix that you can use for..of loop
function setUsersToPremium(users) {
for (let user of users) {
user['isPremium'] = true;
}
return users;
}
Comments
You can use .map which creates a new array with your desired result.
const users = [{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
]
const result = users.map(o => ({ isPremium: true}))
console.log(result)
Comments
const users = [{
isPremium: false
}, {
isPremium: false
}, {
isPremium: false
}, {
isPremium: false
}, {
isPremium: false
}, ]
function setUsersToPremium(users) {
// users is an array of user objects.
// each user object has the property 'isPremium'
// set each user's isPremium property to true
// return the users array
for (let key of users) {
key.isPremium = true;
}
return users;
}
setUsersToPremium(users);
Comments
Here is also a simple way without using any loops. Since, you want to change the value of all the isPremium property of the objects, you can stringify the JSON array and then replace the false value with true then parse it back to an array. As simple as that:
const users = [
{'isPremium': false},
{'isPremium': false},
{'isPremium': false},
{'isPremium': false},
{'isPremium': false},
];
var res = JSON.parse(JSON.stringify(users).replace(/false/g,'true'));
console.log(res);
4 Comments
=== will give false for references that are supposed to point to the same object.const users = [
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false},
{isPremium: false}
];
/** setUsersToPremium()
* param: users, an array of objects
* sets each user's isPremium property to true
* returns users array
**/
function setUsersToPremium() {
for (let i = 0, max = users.length; i < max; i++) {
users[i].isPremium = true;
}
}
setUsersToPremium();
console.log(users);
A couple of notes:
- Unnecessary to pass the users array of objects to function nor does the function need to return this array to effect desired changes.
- Using a simple for-loop may seem old-fashioned but it still works well.