猿问

将对象的属性值添加到对象数组

我有一个数组对象和一个具有与数组相同数量的属性的对象。


我需要将对象的属性值附加到数组的每个对象。填充值属性


大批


0: {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"}

1: {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}

[n]

目的


0: "23"

1: "34"

2: "sdfds"

[n]

代码


this.dataForm.forEach( (el) => {

    Object.entries(formObject).forEach(([key, value]) => {

      el.value = value

    })

})

你能帮忙吗?


预期结果


0: {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: 23}

1: {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: 34}


慕容3067478
浏览 466回答 2
2回答

芜湖不芜

您可以使用 map()const arr = [{number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"},{number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}]const obj = {  0: "23",  1: "34",  2: "sdfds"}const res = arr.map((x,i) => ({...x,value:obj[i]}))console.log(res)

吃鸡游戏

您可以使用 forEach 并在每次迭代中访问实际索引以分配正确的值:data = [    {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"},    {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}  ]values = ["23", "34"]// Here you do the magic :)data.forEach((row, index) => row.value = values[index])
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答