当在下面提到的confList数组中找到匹配时,我想基于RoundNo比较两个数组对象,并替换List数组中的confNo值:
const List = [{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3",
"confNo": "yyyy"
},
{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3",
"confNo": "yyyy"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2",
"confNo": "yyyy"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2",
"confNo": "yyyy"
}
]
let confList = [{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3",
"confNo": "ABNCKJ"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "3",
"confNo": "BJWVFRHE"
}
]
List.map(function(x) {
let result = confNoList.filter(a1 => a1.RoundNo == x.RoundNo);
console.log("result" + JSON.stringify(result));
x.confNo = result.confNo;
return x
})
console.log("List :" + JSON.stringify(List));
不确定为什么confNo没有打印在输出中。上面代码的输出:
List : [
{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3"
},
{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2"
}
]
预期输出:
[{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3",
"confNo": "ABNCKJ"
},
{
"Round": "1",
"RoundNo": "R1-1",
"noOfResubmit": "3",
"confNo": "ABNCKJ"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2",
"confNo": "BJWVFRHE"
},
{
"Round": "1",
"RoundNo": "R1",
"noOfResubmit": "2",
"confNo": "BJWVFRHE"
}
]
因为您已经计算出find
和filter
,所以这里有一种方法,通过创建一个查找映射,您可以稍后在恒定时间内访问该映射(而不必执行find
或filter
完成的嵌套循环)。查找基本上是由RoundNo进行的分组。可以使用数组reduce
对进行分组
const List = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }]
let confList = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "ABNCKJ" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "3", "confNo": "BJWVFRHE" }]
const lookup = confList.reduce((acc,curr) => {
acc[curr.RoundNo] = curr
return acc
},{})
console.log(lookup)
const newList = List.map((x) => {
return {...x,confNo:lookup[x.RoundNo].confNo}
})
console.log(newList)
如果不打算使用它返回的数组,也不要使用map
。如果您想更改现有列表,请使用forEach
或for循环
const List = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }]
let confList = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "ABNCKJ" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "3", "confNo": "BJWVFRHE" }]
const lookup = confList.reduce((acc,curr) => {
acc[curr.RoundNo] = curr
return acc
},{})
List.forEach((x) => {
x.confNo = lookup[x.RoundNo].confNo
})
console.log(List)
为了完成,我将添加find
以及
const List = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "2", "confNo": "yyyy" }]
let confList = [{ "Round": "1", "RoundNo": "R1-1", "noOfResubmit": "3", "confNo": "ABNCKJ" }, { "Round": "1", "RoundNo": "R1", "noOfResubmit": "3", "confNo": "BJWVFRHE" }]
const newList = List.map((x) => {
const result = confList.find(a1 => a1.RoundNo == x.RoundNo);
return {...x,confNo:result.confNo}
})
console.log(newList)
//or forEach if you want to mutate
/**
List.forEach((x) => {
const result = confList.find(a1 => a1.RoundNo == x.RoundNo);
x.confNo = result.confNo
})
console.log(List)
**/