根据值过滤两个数组以删除重复项
Filtering Two Arrays based on values to remove duplicates
我有两个数组,我试图从中过滤特定值。在 arr1 我有我的初始数据集。在 arr2 中,我有要从 arr1.
中删除的项目
arr1 中的数据只有在与 属性 的 arr2.item 都匹配时才应删除和 arr2.itemID
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
我之前试过的代码是
const filtered_arr = arr2.map(i => JSON.stringify(i.item, i.itemID));
const NewItems = arr1.filter(i => !filtered_arr.includes(JSON.stringify(i.item, i.itemID)));
console.log(NewItems);
但是,我没有得到正确的输出。
我正在寻找的输出是:
[
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
]
感谢您的帮助
您的代码没有机会,因为:
JSON.stringify(i.item, i.itemID)
没有正确使用。 JSON.stringify()
有一个像这样的函数签名:
JSON.stringify(value, replacer, space)
你不能像以前那样只传递两条数据。在对该单个对象调用 JSON.stringify()
之前,您必须将这两个数据组合成一个数组或对象。但是,正如您将在下面的第二个解决方案中看到的那样,您不需要 JSON 来对可直接比较的两个值进行规范表示。因为都是字符串,所以直接加上分隔符就可以了,直接比较就可以了
在解决此类问题时,我通常会在比较多个数组时尝试避免 NxM 查找,方法是使用 Map
或 Set
以提高查找效率。比暴力搜索多几行代码,但如果数组变大,效率会高得多。
这是一种方法,使用 Map
对象进行高效查找:
function filterArray(source, removals) {
const removeMap = new Map(removals.map(entry => [entry.itemID, entry.item]));
return source.filter(entry => {
const match = removeMap.get(entry.itemID);
return !match || match !== entry.item;
});
}
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
console.log(filterArray(arr1, arr2));
并且,使用 Set
进行高效查找的稍短版本将项目和 itemID 组合成 Set
对象中的单个规范字符串:
function filterArray2(source, removals) {
const removeSet = new Set(removals.map(entry => `${entry.item}-${entry.itemID}`));
return source.filter(entry => !removeSet.has(`${entry.item}-${entry.itemID}`));
}
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
console.log(filterArray2(arr1, arr2));
此版本假设 itemID
不能以 -
开头,项目名称不能以 -
结尾。如果可以,那么您只需要一个不同的分隔符即可,该分隔符不允许出现在项目名称的末尾或项目 ID 的开头。
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
let arr3=[];
arr2.filter((ele,ind)=>{
arr1.filter((ele2,ind2)=>{
if(ele.item===ele2.item && ele.itemID===ele2.itemID){
arr3.push(arr1[ind2]);
}
})
})
console.log(arr3);
我有两个数组,我试图从中过滤特定值。在 arr1 我有我的初始数据集。在 arr2 中,我有要从 arr1.
中删除的项目arr1 中的数据只有在与 属性 的 arr2.item 都匹配时才应删除和 arr2.itemID
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
我之前试过的代码是
const filtered_arr = arr2.map(i => JSON.stringify(i.item, i.itemID));
const NewItems = arr1.filter(i => !filtered_arr.includes(JSON.stringify(i.item, i.itemID)));
console.log(NewItems);
但是,我没有得到正确的输出。
我正在寻找的输出是:
[
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
]
感谢您的帮助
您的代码没有机会,因为:
JSON.stringify(i.item, i.itemID)
没有正确使用。 JSON.stringify()
有一个像这样的函数签名:
JSON.stringify(value, replacer, space)
你不能像以前那样只传递两条数据。在对该单个对象调用 JSON.stringify()
之前,您必须将这两个数据组合成一个数组或对象。但是,正如您将在下面的第二个解决方案中看到的那样,您不需要 JSON 来对可直接比较的两个值进行规范表示。因为都是字符串,所以直接加上分隔符就可以了,直接比较就可以了
在解决此类问题时,我通常会在比较多个数组时尝试避免 NxM 查找,方法是使用 Map
或 Set
以提高查找效率。比暴力搜索多几行代码,但如果数组变大,效率会高得多。
这是一种方法,使用 Map
对象进行高效查找:
function filterArray(source, removals) {
const removeMap = new Map(removals.map(entry => [entry.itemID, entry.item]));
return source.filter(entry => {
const match = removeMap.get(entry.itemID);
return !match || match !== entry.item;
});
}
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
console.log(filterArray(arr1, arr2));
并且,使用 Set
进行高效查找的稍短版本将项目和 itemID 组合成 Set
对象中的单个规范字符串:
function filterArray2(source, removals) {
const removeSet = new Set(removals.map(entry => `${entry.item}-${entry.itemID}`));
return source.filter(entry => !removeSet.has(`${entry.item}-${entry.itemID}`));
}
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
console.log(filterArray2(arr1, arr2));
此版本假设 itemID
不能以 -
开头,项目名称不能以 -
结尾。如果可以,那么您只需要一个不同的分隔符即可,该分隔符不允许出现在项目名称的末尾或项目 ID 的开头。
let arr1 = [
{ item: 'Apple', itemID: '8189', pricePaid: 0.0762235 },
{ item: 'Apple', itemID: '8188', pricePaid: 0.0762235 },
{ item: 'Pear', itemID: '7144', pricePaid: 1.0483152 },
{ item: 'Berry', itemID: '378', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '377', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '376', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '375', pricePaid: 0.002027 },
{ item: 'Berry', itemID: '374', pricePaid: 0.002027 },
]
let arr2 = [
{ item: 'Berry', itemID: '378' },
{ item: 'Berry', itemID: '377' },
{ item: 'Berry', itemID: '376' },
{ item: 'Berry', itemID: '374' },
]
let arr3=[];
arr2.filter((ele,ind)=>{
arr1.filter((ele2,ind2)=>{
if(ele.item===ele2.item && ele.itemID===ele2.itemID){
arr3.push(arr1[ind2]);
}
})
})
console.log(arr3);