JS - 嵌套循环 - 按键比较 2 个数组,返回 4 个新数组,每个数组匹配和取消匹配



为问题做了一个演示-

阵列-

const arr1 = [
{
key: "daniel|21",
name: "daniel",
age: 21,
city: "NYC"
},
{
key: "kosta|28",
name: "kosta",
age: 28,
city: "NYC"
}
];
const arr2 = [
{
key: "daniel|21",
name: "daniel",
age: 21,
city: "TLV"
},
{
key: "simon|24",
name: "simon",
age: 24,
city: "NYC"
},
{
key: "david|22",
name: "david",
age: 22,
city: "NYC"
}
];

用于分配的空数组-

let arr1Match = [];
let arr1Unmatch = [];
let arr2Match = [];
let arr2Unmatch = [];

对于环路-

for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
const arr1Key = arr1[i].key;
const arr2Key = arr2[j].key;
if (arr1Key === arr2Key) {
arr1Match.push(arr1[i])
arr2Match.push(arr2[i])
} else {
arr1Unmatch.push(arr1[i])
arr2Unmatch.push(arr2[i])
}
}

}

我输出的内容-

console.log(arr1Match);
console.log(arr1Unmatch);
console.log(arr2Match);
console.log(arr2Unmatch);

输出-

arr1Match:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "NYC" }]

arr1不匹配:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "NYC" }, { "key": "daniel|21", "name": "daniel", "age": 21, "city": "NYC" }, { "key": "kosta|28", "name": "kosta", "age": 28, "city": "NYC" }, { "key": "kosta|28", "name": "kosta", "age": 28, "city": "NYC" }, { "key": "kosta|28", "name": "kosta", "age": 28, "city": "NYC" }]

arr2Match:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "TLV" }]

arr2不匹配:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "TLV" }, { "key": "daniel|21", "name": "daniel", "age": 21, "city": "TLV" }, { "key": "simon|24", "name": "simon", "age": 24, "city": "NYC" }, { "key": "simon|24", "name": "simon", "age": 24, "city": "NYC" }, { "key": "simon|24", "name": "simon", "age": 24, "city": "NYC" }]

所需输出-

arr1Match:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "NYC" }]

arr1不匹配:[{ "key": "kosta|28", "name": "kosta", "age": 28, "city": "NYC" }]

arr2Match:[{ "key": "daniel|21", "name": "daniel", "age": 21, "city": "TLV" }]

arr2不匹配:[{ "key": "simon|24", "name": "simon", "age": 24, "city": "NYC" }, { "key": "david|22", "name": "david", "age": 22, "city": "NYC" }]

我需要循环遍历数组中的所有值,并返回每个值的所有匹配和取消匹配。如果有匹配的,我需要将属性从arr2分类到arr1

我不太确定我是否理解你的问题,这是你想要的结果吗?

const arr1 = [{
key: "daniel|21",
name: "daniel",
age: 21,
city: "NYC"
},
{
key: "kosta|28",
name: "kosta",
age: 28,
city: "NYC"
}
];
const arr2 = [{
key: "daniel|21",
name: "daniel",
age: 21,
city: "TLV"
},
{
key: "simon|24",
name: "simon",
age: 24,
city: "NYC"
},
{
key: "david|22",
name: "david",
age: 22,
city: "NYC"
}
];
const arr1Keys = arr1.map(a => a.key);
const arr2Keys = arr2.map(a => a.key);
const keys = [...arr1Keys, ...arr2Keys];
const repeat = keys.filter((o, i) => keys.indexOf(o) !== i);
const unique = [...new Set(keys)].filter(o => !repeat.includes(o));
const arr1Match = arr1.filter(a => repeat.includes(a.key));
const arr1Unmatch = arr1.filter(a => unique.includes(a.key));
const arr2Match = arr2.filter(a => repeat.includes(a.key));
const arr2Unmatch = arr2.filter(a => unique.includes(a.key));
const arr1Match2 = arr1Match.map(a1 => {
let obj = arr2Match.find(a2 => a2.key == a1.key);
return {...obj, city: obj.city}
});
const arr1Match3 = arr1Match.map(a1 => arr2Match.find(a2 => a2.key == a1.key));
console.log('arr1Match before', JSON.stringify(arr1Match));
console.log('arr1Match overwrite city', JSON.stringify(arr1Match2));
console.log('arr1Match overwrite all', JSON.stringify(arr1Match3));
console.log('arr1Unmatch', JSON.stringify(arr1Unmatch));
console.log('arr2Match', JSON.stringify(arr2Match));
console.log('arr2Unmatch', JSON.stringify(arr2Unmatch));

查找常见对象并更新它们的方法。

const
update = (a, b) => {
const
fn = o => o.key,
references = Object.fromEntries(a.map(o => [fn(o), o]));
b.forEach(o => {
const target = references[fn(o)];
if (target) Object.assign(target, o);
});
}
array1 = [{ key: "daniel|21", name: "daniel", age: 21, city: "NYC" }, { key: "kosta|28", name: "kosta", age: 28, city: "NYC" }],
array2 = [{ key: "daniel|21", name: "daniel", age: 21, city: "TLV" }, { key: "simon|24", name: "simon", age: 24, city: "NYC" }, { key: "david|22", name: "david", age: 22, city: "NYC" }];
update(array1, array2);
console.log(array1); // inclusive the one with updates!
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以获取公共密钥并获取每个数组的分区。

const
match = (a, b, fn) => {
const 
getCommon = (a, b) => {
const c = {};
a.forEach(o => c[fn(o)] = false);
b.forEach(o => c[fn(o)] = fn(o) in c);
return c;
};
common = getCommon(a, b, fn),
getParts = (a, c) => a.reduce((r, o) => (r[+!common[fn(o)]].push(o), r), [[], []]);

return [
getParts(a, common),
getParts(b, common)
];
}
array1 = [{ key: "daniel|21", name: "daniel", age: 21, city: "NYC" }, { key: "kosta|28", name: "kosta", age: 28, city: "NYC" }],
array2 = [{ key: "daniel|21", name: "daniel", age: 21, city: "TLV" }, { key: "simon|24", name: "simon", age: 24, city: "NYC" }, { key: "david|22", name: "david", age: 22, city: "NYC" }],
[
[arr1Match, arr1Unmatch],
[arr2Match, arr2Unmatch]
] = match(array1, array2, o => o.key);
console.log(arr1Match);
console.log(arr1Unmatch);
console.log(arr2Match);
console.log(arr2Unmatch);
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新