Glory Raj
Glory Raj

Reputation: 17691

filtering the array of objects based on other array of objects

Hi all i have two array of objects like as below

const selectedCodes = [
  { id: 1, modifiedObject: null, originalObject: {id: 23, name: 'test'}},
  { id: 2, modifiedObject: {id: 24, name: 'test2'}, originalObject: null },
  ....
  ....
];

another array of objects like as below

const originalCodes = [
  { id: 23, name: 'test' },
  { id: 24, name: 'test2'},
  { id: 25, name: 'test3' },
  { id: 26, name: 'test4' }
];

I am looking for the results that originalCodes should return these two items

const originalCodes = [
      { id: 25, name: 'test3' },
      { id: 26, name: 'test4' }
    ];

here i would like to filter the results of originalCodes array based on id available from selectedCodes array modifiedObject/ originalObject Id's and it is always only one object per index either modified object and original object will be present.

Could any one please let me know or any ideas on how can i achieve the results, Many thanks in advance.

i have tried below

const removedArrays = selectCodes.reduce((acc, item) => {
   console.log(item)// here i need to verify  with other array of objects
},[{}])

Upvotes: 0

Views: 51

Answers (4)

Moob
Moob

Reputation: 16184

Its not entirely clear what you are looking for. It sounds like you want find originalCodes which do not exist in the selectedCodes. If so then the following may work for you:

const selectedCodes = [
  { id: 1, modifiedObject: null, originalObject: {id: 23, name: 'test'}},
  { id: 2, modifiedObject: {id: 24, name: 'test2'}, originalObject: null }
];

const originalCodes = [
  { id: 23, name: 'test' },
  { id: 24, name: 'test2'},
  { id: 25, name: 'test3' },
  { id: 26, name: 'test4' }
];

const filteredCodes = originalCodes.filter(function(item) {
  return !(selectedCodes.find(element => (element.originalObject && element.originalObject.id) == item.id) || selectedCodes.find(element => (element.modifiedObject && element.modifiedObject.id) == item.id));
});

console.log(filteredCodes);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386520

You could stringify the objects for an exclude object and filter originalCodes.

This approach requires all objects to have the same order of all properties.

const
    selectedCodes = [{ id: 1, modifiedObject: null, originalObject: { id: 23, name: 'test' } }, { id: 2, modifiedObject: { id: 24, name: 'test2' }, originalObject: null }],
    originalCodes = [{ id: 23, name: 'test' }, { id: 24, name: 'test2'}, { id: 25, name: 'test3' }, { id: 26, name: 'test4' }],
    exclude = Object.fromEntries(selectedCodes.map(o => [JSON.stringify(o.originalObject || o.modifiedObject), true])),
    result = originalCodes.filter(o => !exclude[JSON.stringify(o)]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Alan Omar
Alan Omar

Reputation: 4217

const selectedCodes=[{id:1,modifiedObject:null,originalObject:{id:23,name:"test"}},{id:2,modifiedObject:{id:24,name:"test2"},originalObject:null}],originalCodes=[{id:23,name:"test"},{id:24,name:"test2"},{id:25,name:"test3"},{id:26,name:"test4"}];

let result = originalCodes.filter(({id}) => 
    !selectedCodes.find(e => 
    (e.originalObject?e.originalObject.id:e.modifiedObject.id) === id ))


console.log(result)

Upvotes: 1

Yevhen Horbunkov
Yevhen Horbunkov

Reputation: 15530

If the idea is to filter out the originalCodes items that are present as name property in either originalObject or modifiedObject, solution could be as simple as that:

const selectedCodes = [{id:1,modifiedObject:null,originalObject:{id:23,name:"test"}},{id:2,modifiedObject:{id:24,name:"test2"},originalObject:null}],
      originalCodes = [{id:23,name:"test"},{id:24,name:"test2"},{id:25,name:"test3"},{id:26,name:"test4"}],
      
      
      result = originalCodes.filter(({id}) => 
                  !selectedCodes.some(({originalObject, modifiedObject}) => {
                    const {id: id1} = (originalObject||{}),
                          {id: id2} = (modifiedObject||{})
                    return id == id1 || id == id2
                  })
               )
      
console.log(result)
.as-console-wrapper{min-height:100%;}

Upvotes: 2

Related Questions