我在互联网上搜索了很多,我认为这将是一个非常简单的任务,但我没有找到任何解决方案
这是我的两个对象数组
第一个数组
[
{ id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 },
{ id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 },
{ id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 },
{ id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 },
{ id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 },
{ id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 },
{ id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 }
]
第二个数组
[
{ id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 },
{ id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 },
{ id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }
]
我想在两个对象数组之间找到区别,我希望得到像这样的结果
第一个数组 - 第二个数组=(第一个数组中存在但第二个数组中没有的对象数组)
[ { id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 },
{ id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 },
{ id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 },
{ id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 } ]
我尝试过许多方法,比如像这样的过滤器
let c = existingBillPlans.filter(item =>
!billPlans.some(other => item.x == other.x));
但它给出了null数组。无法理解我应该做什么。
答案 0 :(得分:1)
你几乎在那里,使用id
代替x
existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));
<强>演示强>
var existingBillPlans = [{
id: 2,
fees: 10000,
name: 'Yearly Plan',
cycle: 12
},
{
id: 3,
fees: 1500,
name: 'Two Months Plan',
cycle: 2
},
{
id: 4,
fees: 2500,
name: 'Three Months Plan',
cycle: 3
},
{
id: 5,
fees: 3000,
name: 'Four Months Plan',
cycle: 4
},
{
id: 181,
fees: 4000,
name: 'Five Months Plan',
cycle: 5
},
{
id: 182,
fees: 5000,
name: 'Six Months Plan',
cycle: 6
},
{
id: 183,
fees: 6000,
name: 'Seven Months Plan',
cycle: 7
}
]
var billPlans = [{
id: 2,
fees: 10000,
name: 'Yearly Plan',
cycle: 12
},
{
id: 3,
fees: 1500,
name: 'Two Months Plan',
cycle: 2
},
{
id: 4,
fees: 2500,
name: 'Three Months Plan',
cycle: 3
}
]
var output = existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));
console.log(output);
&#13;
答案 1 :(得分:1)
我想你想检查每个对象的属性。
要检查所有对象的属性,请使用every
方法。
let first_array = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }, { id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 }, { id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 }, { id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 }, { id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 } ], second_array = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 } ]
let result = first_array
.filter(item => !second_array
.find(other => Object.keys(other)
.every(prop => item[prop] == other[prop]))
);
console.log(result);
&#13;
答案 2 :(得分:1)
您可以使用ids
创建.map()
数组,然后应用.filter()
let result = (
(array, ids) => array.filter(({ id }) => !ids.includes(id)))(a1, a2.map(({ id }) => id)
);
<强>演示:强>
let a1 = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }, { id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 }, { id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 },{ id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 },{ id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 } ];
let a2 = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 } ];
let result = (
(array, ids) => array.filter(({ id }) => !ids.includes(id)))(a1, a2.map(({ id }) => id)
);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
或者你可以像这样使用filter()
:
let result = a1.filter(({ id }) => !a2.find(o => o.id == id));
<强>演示:强>
let a1 = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 },{ id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 }, { id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 }, { id: 5, fees: 3000, name: 'Four Months Plan', cycle: 4 }, { id: 181, fees: 4000, name: 'Five Months Plan', cycle: 5 },{ id: 182, fees: 5000, name: 'Six Months Plan', cycle: 6 }, { id: 183, fees: 6000, name: 'Seven Months Plan', cycle: 7 } ];
let a2 = [ { id: 2, fees: 10000, name: 'Yearly Plan', cycle: 12 }, { id: 3, fees: 1500, name: 'Two Months Plan', cycle: 2 },{ id: 4, fees: 2500, name: 'Three Months Plan', cycle: 3 } ];
let result = a1.filter(({ id }) => !a2.find(o => o.id == id));
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;