Home > Enterprise >  compare two different array of objects and filter out the item which is having same ID from one arra
compare two different array of objects and filter out the item which is having same ID from one arra

Time:12-22

I am trying to compare and filter out the two different array of objects I need to check against all the elements in both the array of objects and need to filter out the item which is having the same ID, meaning I need to filter the identical Item from only one array another array should have that item. I have tried using filter some and find methods all are giving me the same element(id) from both the array, but I need to filter out from my 2nd array only.

// Example :

// Lets's say I am having following array of objects:

const a = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
  { id: '123456', description: 'PrdThree', version: '2', categories: '--' },
];

const b = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
];

// I am trying to get something like below:

const res = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
  { id: '123456', description: 'PrdThree', version: '2', categories: '--' },
];

Basically I need to check for each and every element and I need to ignore the items which is same in the array b only I should get the items in array a , and also I don't want that item from const b to be removed, I need to check both the arrays and if it matches I need to go with only A at the same time items inside the B should not be removed or deleted.

Solution I tried below giving me the same item from both the list. Could any one help me out how can I achieve this? Thanks in advance`

const a = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
  { id: '123456', description: 'PrdThree', version: '2', categories: '--' },
];

const b = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
];

let result = a.filter(o => !b.some(v => v.id === o.id));

console.log(result);

CodePudding user response:

you can try this :

let arr = []

    let res = a.filter(o => !b.some(v => v.id === o.id))
    let res2 = a.filter(o => b.some(x => x.id == o.id))
    arr.push(...res2 , ...res)

CodePudding user response:

You could take a closure over a Set and filter an array of all items.

const
    a = [{ id: '1234', description: 'PrdOne', version: '3', categories: '--' }, { id: '12345', description: 'PrdTwo', version: '2', categories: '--' }, { id: '123456', description: 'PrdThree', version: '2', categories: '--' }],
    b = [{ id: '1234', description: 'PrdOne', version: '3', categories: '--' }, { id: '12345', description: 'PrdTwo', version: '2', categories: '--' }],
    result = [...a, ...b].filter(
        (s => ({ id }) => !s.has(id) && s.add(id))
        (new Set)
    );

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

CodePudding user response:

    const a = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
  { id: '123456', description: 'PrdThree', version: '2', categories: '--' },
];

const b = [
  { id: '1234', description: 'PrdOne', version: '3', categories: '--' },
  { id: '12345', description: 'PrdTwo', version: '2', categories: '--' },
];
function some(el){
    const objectKeys=Object.keys(el);
    for(let elemnets of b){
        if(objectKeys.length!=Object.keys(elemnets).length)continue;
        let found=1;
        for(let key of objectKeys){
            if(el[key]!=elemnets[key]){
                found=0;
                break;
            }
        }
        if(found)return true;
    }
    return false
}
let result = a.filter((el)=>{
    return some(el);
});

console.log(result);

try this

  • Related