0

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
},[{}])
5
  • please add your try. Commented Dec 9, 2020 at 21:34
  • I am trying to do with reduce like this const removedArrays = selectedCodes.reduce((acc) => { console.log(acc) },[{}]) but could not be able to know how to loop through other array Commented Dec 9, 2020 at 21:41
  • @NinaScholz i updated my question Commented Dec 9, 2020 at 21:46
  • does the (nested) objects have the same object reference? Commented Dec 9, 2020 at 21:54
  • the nested objects looks like same as original objects Commented Dec 9, 2020 at 21:56

4 Answers 4

2

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%;}

Sign up to request clarification or add additional context in comments.

2 Comments

thanks and is it possible to return the originalCodes with out creating new variable
you have mentioned react.js among your tags, so I presumed you would prefer immutable approach (which is usually the case to manipulate the state). however, if having extra variable is an issue for some reason (and reassign source variable to resulting array is not an option either), you may come up with some combination of for(..-loop coupled with .splice(), dropping the items that evaluated truthy by above .some()-method.
1

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)

Comments

1

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

Comments

1

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);

2 Comments

thanks for the suggestion i would need the result completely reverse (i.e) would like to get the objects contains 25 and 26
Updated. Just switched the logic by appending ! (not)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.