4

I have searched a lot on internet and I thought this would be a really simple task but I have not found any solution for this

These are my two array of objects

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 } 
]

I want to get difference between two array of Objects and I want result something like this

First array - Second array = ( array of objects which are there in first array but not there in second array )

[ { 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 } ]

I have tried many many methods like filter like this

     let c = existingBillPlans.filter(item => 
!billPlans.some(other => item.x == other.x));

but it is giving null array . Not able to understand what I should do .

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Vikas
  • 975
  • 1
  • 10
  • 34

3 Answers3

2

I suppose you want to check every object's property.

For checking all the object's properties use every method.

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);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
2

You can create an array of ids using .map() and then apply .filter()

let result = (
  (array, ids) => array.filter(({ id }) => !ids.includes(id)))(a1, a2.map(({ id }) => id)
);

Demo:

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Alternatively you can use filter() like this:

let result = a1.filter(({ id }) => !a2.find(o => o.id == id));

Demo:

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
1

You were almost there, use id instead of x

existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));

Demo

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);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94