我有兩個陣列,我試圖從中過濾特定值。在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 },
]
感謝您的任何幫助
uj5u.com熱心網友回復:
您的代碼沒有機會,因為:
JSON.stringify(i.item, i.itemID)
沒有正確使用。 JSON.stringify()有這樣的函式簽名:
JSON.stringify(value, replacer, space)
你不能像以前那樣只傳遞兩條資料。JSON.stringify()在呼叫單個物件之前,您必須將這兩條資料組合成一個陣列或物件。但是,正如您將在下面的第二個解決方案中看到的那樣,您不需要 JSON 來對可直接比較的兩個值進行規范表示。由于它們都是字串,您可以在它們之間添加分隔符,然后您可以直接比較字串。
在我對此類問題的解決方案中,我通常在比較多個陣列時嘗試避免 NxM 查找,方法是使用 aMap或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用于高效查找的版本,它將 item 和 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));
這個版本只是假設 anitemID不能以 a 開頭,-并且專案名稱不能以 a 結尾-。如果可以,那么您只需要一個不同的分隔符,該分隔符不允許出現在專案名稱的末尾或 itemID 的開頭。
uj5u.com熱心網友回復:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462151.html
標籤:javascript 节点.js 排序 筛选
