Javascript ES6/ES5 find in array and change

前端 未结 8 1323
后悔当初
后悔当初 2020-12-12 10:52

I have an array of objects. I want to find by some field, and then to change it:

var item = {...}
var items = [{id:2}, {id:2}, {id:2}];

var foundItem = item         


        
8条回答
  •  天命终不由人
    2020-12-12 10:54

    One-liner using spread operator.

     const updatedData = originalData.map(x => (x.id === id ? { ...x, updatedField: 1 } : x));
    

提交回复
热议问题