一道js问题求助一下
//1.需求遍历listData,将数组key和对象key相匹配,如果匹配则将对象的value赋值给数组的value let listData= [
{ name: "张三:", key: "authenNo",value:''},
{ name: "李四:", key: "extendedRepair" ,value:''},
{ name: "赵恶:", key: "newCarSaleDate", value:''},
{ name: "王五:", key: "lastAuthenDate" ,value:''},
{ name: "笨蛋:", key: "gap" ,value:''},
];
let Obj={
authenNo:'我才不是张三呢',
extendedRepair:'我才不是李四',
lastAuthenDate:'我才不是王五',
newCarSaleDate:'我才不是赵恶'
},
最后的操作结果应为:
listData= [
{ name: "张三:", key: "authenNo",value:'我才不是张三呢'}, { name: "李四:", key: "extendedRepair" ,value:'我才不是李四'},
{ name: "赵恶:", key: "newCarSaleDate", value:'我才不是赵恶'},
{ name: "王五:", key: "lastAuthenDate" ,value:'我才不是王五'},
{ name: "笨蛋:", key: "gap" ,value:''},
];
回答:
//不支持 ?? 就用 || 代替下//listData.forEach(item => item.value = Obj[item.key] ?? item.value)
listData.forEach(item => item.value = Obj[item.key] || item.value)
以上是 一道js问题求助一下 的全部内容, 来源链接: utcz.com/p/936081.html