Js change object inside array in for each loop

后端 未结 5 1753
广开言路
广开言路 2020-12-18 19:43

I want to change the current object in for each loop and it does not work, Why it is not working and how can i do this?

var arr = [{num: 1}, {num: 2}];

arr.         


        
5条回答
  •  Happy的楠姐
    2020-12-18 20:26

    You are not working on the object so changes are not transferred.

    You can do this instead:

    arr.forEach(function(item, ind, array) {
      array[ind] = {somethingElse: 1}
    });
    

提交回复
热议问题