Js change object inside array in for each loop

后端 未结 5 1759
广开言路
广开言路 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条回答
  •  眼角桃花
    2020-12-18 20:37

    Another variety to the list of answers

    var arr = [{num: 1}, {num: 2}];
    
    arr.forEach(function(item) {
      item.something = 2;//setting the value
      delete item.num;//deleting the num from the object
    });
    

提交回复
热议问题