I have two arrays that I am trying to filter specific values from them. In arr1 i have my intial dataset. in arr2 I have the items I want to delete from arr1.
The data in arr1 should only be deleted if it matches both the property's arr2.item and 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' },
]
The code that I tried before was
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);
However, I am not getting the correct output.
The output I am looking for is:
[
{ 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 },
]
Thank you for any help