//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);
我想知道如何遍历一个对象数组并将其值从false更改为true。这段代码的结果是
[true, true, true, true, true ]
但我想做的是改变每个
[isPremium: true]
我想知道我做了什么让我无法访问此值。
答案 0 :(得分:4)
您必须设为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);
理想方法是使用forEach
代替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);
如果您不想影响原始数组,可以使用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);
答案 1 :(得分:2)
您正在为数组使用for..in
循环,因此您将获得array (1,2,3...)
的索引
要解决此问题,您可以使用for..of
循环
function setUsersToPremium(users) {
for (let user of users) {
user['isPremium'] = true;
}
return users;
}
答案 2 :(得分:1)
您可以使用.map
创建一个包含所需结果的新数组。
const users = [{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
{
isPremium: false
},
]
const result = users.map(o => ({ isPremium: true}))
console.log(result)
答案 3 :(得分:0)
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);
答案 4 :(得分:0)
这也是一种不使用任何循环的简单方法。因为,您想要更改对象的所有isPremium
属性的值,您可以对JSON数组进行字符串化,然后将false
值替换为true
,然后将其解析回数组。就这么简单:
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);
答案 5 :(得分:0)
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);
几点说明: