reactjs 比较两个不同的对象数组并筛选出项[duplicate]

rqcrx0a6  于 2022-12-26  发布在  React
关注(0)|答案(4)|浏览(186)
    • 此问题在此处已有答案**:

How to remove all duplicates from an array of objects?(76个答案)
2天前关闭。
我尝试比较并过滤出两个不同的对象数组,我需要检查两个对象数组中的所有元素,并需要过滤出具有相同ID的项,这意味着我只需要从一个数组中过滤出相同的项,另一个数组应该具有该项。(id),但我只需要从第二个数组中过滤掉。

// Example :

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

const a = [
  { id: '1234', description: 'PrdOne' },
  { id: '12345', description: 'PrdTwo'},
  { id: '123456', description: 'PrdThre },
];

const b = [
  { id: '1234', description: 'PrdOne'},
  { id: '12345', description: 'PrdTwo'},
];

// I am trying to get something like below:

const res = [
  { id: '1234', description: 'PrdOne'},
  { id: '12345', description: 'PrdTwo' },
  { id: '123456', description: 'PrdThree' },
];

let result = a.filter(o => !b.some(v => v.id === o.id));
控制台日志(结果);

ki1q1bka

ki1q1bka1#

你可以试试这个:

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

szqfcxe22#

您可以在Set上进行闭包,并过滤所有项的数组。

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

wgx48brx3#

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

试试这个

e37o9pze

e37o9pze4#

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

const concatArr = a.concat(b); // will contain all the entries from both array

const ids = concatArr.map(o => o.id); // ['1234', '12345', '123456', '1234', '12345']

const uniqueIds = [...new Set(ids)]; // ['1234', '12345', '123456']

const filtered = concatArr.filter((obj) => {
    if(uniqueIds.includes(obj['id'])){
        uniqueIds.splice(uniqueIds.indexOf(obj['id']), 1);
        return true;
    }
    return false;
});

console.log(filtered); // [{"id":"1234","description":"PrdOne","version":"3"},{"id":"12345","description":"PrdTwo","version":"2"},{"id":"12345","description":"PrdTwo","version":"2"}]

相关问题