循环对象数组javascript

时间:2018-03-18 07:46:26

标签: javascript object

//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]

我想知道我做了什么让我无法访问此值。

6 个答案:

答案 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);

几点说明:

  • 不必将用户数组对象传递给函数也不行 该函数需要返回此数组以实现所需的更改。
  • 使用简单的for循环可能看起来很老套,但它仍然可以正常运行。