How to replace an item in an array with JavaScript?

后端 未结 26 2247
执笔经年
执笔经年 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条回答
  •  猫巷女王i
    2020-11-29 16:09

    Here's a one liner. It assumes the item will be in the array.

    var items = [523, 3452, 334, 31, 5346]
    var replace = (arr, oldVal, newVal) => (arr[arr.indexOf(oldVal)] = newVal, arr)
    console.log(replace(items, 3452, 1010))

提交回复
热议问题