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"
}
- 以前的做法定义一个全量对象
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
}
为什么当时用了两个数组,脑子进水了。。。
上一篇: 常见php数据文件缓存类汇总
下一篇: ThinkPHP模版引擎之变量输出详解