我有这个对象
{id: 4450, name: "product name", stock_status: "IN_STOCK", item: 32}
和这个对象数组
[{value: "31", label: "1"}, {value: "32", label: "2"}]
es6中是否有一种简单的方法将第一个对象的“ item”属性与数组中对象的“ value”属性进行匹配,并用“ label”的值返回第一个对象,从而替换item的值像这样:
{id: 4450, name: "product name", stock_status: "IN_STOCK", item: "2"}
答案 0 :(得分:0)
如果您使用传播运算符,也许会有用
const obj = {id: 4450, name: "product name", stock_status: "IN_STOCK", item: 32}
const arr = [{value: "31", label: "1"}, {value: "32", label: "2"}]
// arr[i] 'i' is index of object in array
let newObj = { ...obj, item: (arr[1].label)}