How to replace an item in an array with JavaScript?

后端 未结 26 2230
执笔经年
执笔经年 2020-11-29 15:33

Each item of this array is some number.

var items = Array(523,3452,334,31, ...5346);

How do I replace some number in with array with a new on

26条回答
  •  孤城傲影
    2020-11-29 16:00

    Well if anyone is interresting on how to replace an object from its index in an array, here's a solution.

    Find the index of the object by its id:

    const index = items.map(item => item.id).indexOf(objectId)
    

    Replace the object using Object.assign() method:

    Object.assign(items[index], newValue)
    

提交回复
热议问题