任务:使用for循环遍历数组,并向所有用户添加10点数

输出提示我位置0的人没有获得10个学分,但是在输出中,我看到增加了10个学分(60);

我有点困惑。这个问题看似愚蠢,但我只需要重新评估。

var users = [
    {name: 'asdfasdf', credits: 50},
    {name: 'asasdfdf', credits: 10},
    {name: 'asdasdffasdf', credits: 11},
    {name: 'weewrwe', credits: 1},
    {name: 'werwer', credits: 190},
    {name: 'sdfsdf', credits: 100},
    {name: 'dfgdfg', credits: 0},
];

for(var i = 0; i < users.length; i++) {
  var obj = users[i];
}
console.log((obj.credits)+ 10);
评论
樱小桃~
樱小桃~
var users = [
    {name: 'asdfasdf', credits: 50},
    {name: 'asasdfdf', credits: 10},
    {name: 'asdasdffasdf', credits: 11},
    {name: 'weewrwe', credits: 1},
    {name: 'werwer', credits: 190},
    {name: 'sdfsdf', credits: 100},
    {name: 'dfgdfg', credits: 0},
];

function addCredits(users, credit){
   return users.map(el => {
      el.credits +=  credit
      return el;
   });
}

let result = addCredits(users, 10);

console.log(result);
点赞
评论