簡體   English   中英

比較兩個不同的對象數組並僅從一個數組中過濾出具有相同 ID 的項目

[英]compare two different array of objects and filter out the item which is having same ID from one array only

我正在嘗試比較和過濾掉兩個不同的對象數組我需要檢查兩個對象數組中的所有元素並且需要過濾掉具有相同 ID 的項目,這意味着我需要過濾相同的項目只有一個數組,另一個數組應該有那個項目。 我試過使用一些過濾器並找到所有方法都從兩個數組中給我相同的元素(id),但我只需要從我的第二個數組中過濾掉。

// 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: '--' },
];

基本上我需要檢查每個元素並且我需要忽略數組 b 中相同的項目只有我應該得到數組 a 中的項目,而且我不希望從 const b 中刪除該項目,我需要檢查兩個數組,如果它匹配,我只需要同時使用 A,同時不應刪除或刪除 B 中的項目。

解決方案我在下面嘗試從兩個列表中給我相同的項目。 誰能幫我解決這個問題? 提前致謝`

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

你可以試試這個:

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)

您可以對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; }

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

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM