我有两个列表,如下所示:
https://docs.google.com/gview?embedded=true&url=[doc address]
和
listOne = [
{phone: "123", name: "Joey"},
{phone: "456", name: "Erik"},
{phone: "789", name: "Teddy"},
]
我将如何比较这两个列表,对于每个重复项(listTwo = [
{phone: "123", name: "Joey", accoundId: "ID_001"},
{phone: "456", name: "Erik", accoundId: "ID_006"},
{phone: "5553", name: "Sebastian", accoundId: "ID_010"},
]
和phone
完全匹配),如果存在重复项,则将值name
中与{{ 1}}在listThree
因此:
accountId
JavaScript,Python或Apex解决方案受到高度赞赏
答案 0 :(得分:0)
list1 = [
{"phone": "123", "name": "Joey"},
{"phone": "456", "name": "Erik"},
{"phone": "789", "name": "Teddy"},
]
list2 = [
{"phone": "123", "name": "Joey", "accountid": "ID_001"},
{"phone": "456", "name": "Erik", "accountid": "ID_006"},
{"phone": "5553", "name": "Sebastian", "accountid": "ID_010"},
]
list3=[]
for i in list1:
for j in list2:
if i['name'] == j['name'] and i['phone']==j['phone']:
list3.append(j)
print(list3)
[{'phone': '123', 'name': 'Joey', 'accountid': 'ID_001'}, {'phone': '456', 'name': 'Erik', 'accountid': 'ID_006'}]
答案 1 :(得分:0)
var commonFound = [];
for (var i = 0, len = listOne.length; i < len; i++) {
for (var j = 0, len2 = listTwo.length; j < len2; j++) {
if ((listOne[i].phone == listTwo[j].phone) && (listOne[i].name == listTwo[j].name)) {
commonFound.push(listTwo[j])
}
}
}
console.log(commonFound)
答案 2 :(得分:0)
我们有两个属性需要检查,例如phone
和name
。基于此,我们可以过滤一个数组:
const result = listTwo.filter(f=>
listOne.some(s => s.phone == f.phone && s.name == f.name));
这是一个示例:
let listOne = [
{phone: "123", name: "Joey"},
{phone: "456", name: "Erik"},
{phone: "789", name: "Teddy"},
];
let listTwo = [
{phone: "123", name: "Joey", accoundId: "ID_001"},
{phone: "456", name: "Erik", accoundId: "ID_006"},
{phone: "5553", name: "Sebastian", accoundId: "ID_010"},
];
const result = listTwo.filter(f=>
listOne.some(s => s.phone == f.phone && s.name == f.name));
console.log(result);