欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js循环匹配组合成新对象或js循环组合新数据

程序员文章站 2022-06-29 12:41:13
var Arry=[ {name: "vehicleTravelLicenseCopyBack", id: "a1"}, {name: "vehicleTravelLicenseCopyFront", id: "a2"}, {name: "idCardBack", id: "a3"}, {name: ......

var arry=[

{name: "vehicletravellicensecopyback", id: "a1"},

{name: "vehicletravellicensecopyfront", id: "a2"},

{name: "idcardback", id: "a3"},

{name: "idcardfront", id: "a4"}

]

为了把上面的东西便成下面的

var imgobj={

idcardback: "a1",

idcardfront: "a2",

vehicletravellicensecopyback: "a3",

vehicletravellicensecopyfront: "a4"

}

  1. 以前的做法定义一个全量对象
 img: {
vehicletravellicensecopyback: void 0,
vehicletravellicensecopyfront: void 0,
idcardback: void 0,
idcardfront: void 0,
}

for (let v of arry) {
for (let n in this.img) {
if (v.name === n) {
imgobj[n] = v.id
}
}
}
 2、我这是有多惨,今天才发现最快是这样的

for (let v of arry) {
imgobj[v.name] = v.id
}
为什么当时用了两个数组,脑子进水了。。。