簡體   English   中英

如何在兩個對象數組之間建立交集

[英]How to make an intersection between two arrays of objects

我正在嘗試在兩個對象數組之間進行交集。

https://codesandbox.io/s/friendly-bohr-v73ob?fbclid=IwAR3yQDnftREENi8lF6wCKYE_F09pimlLgfYca0B_oIPqYYHvbAf4cvnG-n4

const list1 = [
  {
    name: "skinType",
    keys: [
      {
        id: "oily",
        label: "Oily"
      },
      {
        id: "dry",
        label: "Dry"
      }
    ]
  },
  {
    name: "finish",
    keys: [
      {
        id: "oily",
        label: "Oily"
      },
      {
        id: "dry",
        label: "Dry"
      },
      {
        id: "matte",
        label: "Matte"
      },
      {
        id: "natural",
        label: "Natural"
      },
      {
        id: "radiant",
        label: "Radiant / Glow"
      }
    ]
  },
  {
    name: "texture",
    keys: [
      {
        id: "matte",
        labal: "Matte"
      }
    ]
  }
];

const list2 = [
  {
    name: "skinType",
    keys: [
      {
        id: "oily",
        label: "Oily"
      },
      {
        id: "dry",
        label: "Dry"
      },
      {
        id: "gandac",
        label: "mazga"
      }
    ]
  },
  {
    name: "finish",
    keys: [
      {
        id: "oily",
        label: "Oily"
      }
    ]
  }
];

我提出了一個解決方案,但它只能根據對象的名稱密鑰進行交集。 現在我需要根據keys數組中的id進行交集。

const intersection = (list1, list2) => {
  return list2.filter(drp => list1.some(rect => rect.name === drp.name));
};

const result = intersection(react, drupal);

預期結果:

[
  {
    name: "skinType",
    keys: [
      {
        id: "oily",
        label: "Oily"
      },
      {
        id: "dry",
        label: "Dry"
      }
    ]
  },
  {
    name: "finish",
    keys: [
      {
        id: "oily",
        label: "Oily"
      }
    ]
  }
]

 const react = [ { name: "skinType", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" } ] }, { name: "finish", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" }, { id: "matte", label: "Matte" }, { id: "natural", label: "Natural" }, { id: "radiant", label: "Radiant / Glow" } ] }, { name: "texture", keys: [ { id: "matte", labal: "Matte" } ] } ]; const drupal = [ { name: "skinType", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" }, { id: "gandac", label: "mazga" } ] }, { name: "finish", keys: [ { id: "oily", label: "Oily" } ] } ]; var result = []; for(var item1 of react) for(var item2 of drupal) if(item1.name == item2.name){ var keys = item2.keys.filter(x => item1.keys.some(y => y.id === x.id)); result.push({name: item1.name, keys}) break; } console.log(result); 

你關心重復嗎?

 const array1 = [1,2,3,4]; const array2 = [5,6,7,8]; const union = (a1, a2) => [...a1, ...a2]; console.log(union(array1, array2)); 

如果你不想重復

 const array1 = [1,2,3,4,5]; const array2 = [4,5,6,7,8]; const distinct = (array) => array ? array.reduce((results, item) => { if (!results.some((i) => i === item)) { results.push(item); } return results; }, []) : array; const union = (a1, a2) => distinct([...a1, ...a2]); console.log(union(array1, array2)); 

另一種方法是通過首先將數組減少到映射(通過key.id標准),然后通過Object.values()提取映射的值來避免聯合結果中的重復值,以獲得作為數組的並集價值觀

中間映射階段背后的想法是確保結果聯合中的值是key.id唯一的。

在代碼中的解決方案可以如下所示:

 const react=[{name:"skinType",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"}]},{name:"finish",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"},{id:"matte",label:"Matte"},{id:"natural",label:"Natural"},{id:"radiant",label:"Radiant / Glow"}]},{name:"texture",keys:[{id:"matte",labal:"Matte"}]}];const drupal=[{name:"skinType",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"},{id:"gandac",label:"mazga"}]},{name:"finish",keys:[{id:"oily",label:"Oily"}]}]; /* Gather both arrays into a combined array via concat() and reduce() the result to obtain the union. The reduce() produces a mapping, and we obtain the union array via Object.values() to extract the map's values as an array */ const union = Object.values([].concat(drupal, react).reduce((map, item) => { /* For each item of the keys sub-array, update the map with value "key" at key "key.id" given that key.id is the union criteria */ item.keys.forEach(key => { map[key.id] = key; }) return map; }, {})) console.log(union); 

暫無
暫無

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

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