如何在两个对象数组之间建立交集

时间:2019-06-27 05:23:43

标签: javascript

我正在尝试在两个对象数组之间进行相交。

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您在乎重复吗?

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

答案 2 :(得分:0)

另一种方法是通过首先将数组简化为映射(通过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);