我有一个这样的对象:
let INVOICE =
{
id: 1589387651868,
name: "testing",
data: "2020-05-03T21:00:00.000Z",
totalPrice: "40.0",
products: [
{
id: 4,
name: "Lemon ",
description: "fresh",
price: 4,
discount: 0
},
{
id: 7,
name: "Banana",
description: "salat",
price: 11,
discount: 0
},
{
id: 6,
name: "Blueberry",
description: "fresh",
price: 15,
discount: 0
},
{
id: 5,
name: "Cherry",
description: "fresh",
price: 10,
discount: 0
}
]
}
像这样的数组:
let PRODUCTS = [
{
id:1,
name: 'Grapes',
description: 'eating',
price: 5
},
{
id:2,
name: 'Lime',
description: 'eating',
price: 3
},
{
id:3,
name: 'Apple',
description: 'eating',
price: 7
},
{
id:4,
name: 'Lemon ',
description: 'fresh',
price: 4
},
{
id:5,
name: 'Cherry',
description: 'fresh',
price: 10
},
{
id:6,
name: 'Blueberry',
description: 'fresh',
price: 15
},
{
id:7,
name: 'Banana',
description: 'salat',
price: 11
},{
id:8,
name: 'Watermelon',
description: 'salat',
price: 20
},
{
id:9,
name: 'Pineapple',
description: 'salat',
price: 30
},
{
id:10,
name: 'Orange',
description: 'salat',
price: 17
},
{
id:11,
name: 'Avocado',
description: 'eating',
price: 26
}
];
我需要通过 INVOICE.products 过滤 PRODUCTS 数组,因此,我应该得到一个新的 PRODUCTS 数组,其中将不再有 INVOICE.products 数组中的对象。我尝试使用以下方法实现它:
C = A.filter(val => { B.indexOf(val) == -1;});
结果,我得到一个空数组,很可能是由于数组中的对象不同!
即使您修复了 id,您的代码也不会给出所需的结果。你能做这样的事情吗