如何从 JS 数组中过滤数据并创建另一个 JS 数组
2020-10-28
93
我想从 JS 数组中过滤数据并创建另一个 JS 数组。我解释了以下场景。
有两个 JS 数组。它们是
arr1
和
arr2
const arr1 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}];
const arr2 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
},
{
code: "vvg",
dis: "cont15",
note: "Note for cont15"
}];
我们可以在
arr2[1].dis
和
arr2[2].note
和
arr2[4].note
中进行更改。我在下面提到过
const arr2 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
},
{
code: "VVG",
dis: "cont15",
note: "Note for cont15 changed"
}];
一旦我更改了
arr2
,我想将其与
arr1
进行比较并创建
new_arr
并将所有更改的元素推送到
new_arr
。比较应该使用
code
完成。因为
code: "VVG"
有变化。但它不应该包含在新数组中。因为
code: "VVG"
不在
arr1
中。
Expected-output :
[
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
}
]
尝试的代码:
const codesss = new Set(arr1.map(({ code }) => code));
const new_arr = arr2.filter(({ code }) => !codesss.has(code));
console.log(new_arr);
当前输出:
[
{
"code": "VVG",
"dis": "cont15",
"note": "Note for cont15 changed"
}
]
我的
当前输出
不是
预期输出
。请帮我解决这个问题。
2个回答
首先,使用
Array.prototype.reduce
,您可以使用
code
变量作为
key
从
arr1
构建新对象。
基于该对象,您可以使用
Array.prototype.filter
过滤所需的对象。
const arr1 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}];
const arr2 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
},
{
code: "VVG",
dis: "cont15",
note: "Note for cont15 changed"
}];
const arr1Obj = arr1.reduce((acc, cur) => {
acc[cur.code] = cur;
return acc;
}, {});
const result = arr2.filter((item) => arr1Obj[item.code] && !(arr1Obj[item.code].dis === item.dis && arr1Obj[item.code].note === item.note));
console.log(result);
Derek Wang
2020-10-28
const arr1 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}];
const arr2 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
},
{
code: "vvg",
dis: "cont15",
note: "Note for cont15"
}];
const arr2Changed = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
},
{
code: "VVG",
dis: "cont15",
note: "Note for cont15 changed"
}];
const arr1Codes = {};
const newArray = [];
arr1.map(i => arr1Codes[i.code] = 1);
arr2Changed.map((item, index) => {
const arr2Item = arr2[index];
if (arr1Codes[arr2Item.code] && (arr2Item.dis !== item.dis || arr2Item.note !== item.note)) {
newArray.push(item);
}
});
console.log(newArray);
Aison
2020-10-28