为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕容3067478 2021-06-20 16:08:42
我有一个数组对象和一个具有与数组相同数量的属性的对象。我需要将对象的属性值附加到数组的每个对象。填充值属性大批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}
查看完整描述

2 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

您可以使用 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)


查看完整回答
反对 回复 2021-06-24
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

您可以使用 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])


查看完整回答
反对 回复 2021-06-24
  • 2 回答
  • 0 关注
  • 465 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信