简体   繁体   English

比较 2 个对象数组并找到匹配的颜色 id,然后创建一个新数组 Javascript

[英]Compare 2 array's of objects and find matching color id's then create a new array Javascript

I am still learning how to create functions that traverse through arrays.我仍在学习如何创建遍历 arrays 的函数。 I have a tough one that I can not figure ou I have two different arrays of objects.我有一个很难想象的东西,我有两个不同的 arrays 对象。

Array 1:阵列 1:

const data1 = [
  {
    name: 'I am obj one',
    id: 125,
    category: [18],
    colors: [19, 20],
  },
  {
    name: 'I am obj two',
    id: 111,
    category: [18],
    colors: [19, 20],
  },
];

Array 2阵列 2

const data2 = [
  {
    invitation: {
      name: 'Type',
      options: [
        {
          name: 'Show1',
          id: 21,
        },

        {
          name: 'Show2',
          id: 22,
        },
      ],
    },
  },

  {
    color: {
      name: 'Color',

      options: [
        {
          name: 'Gray',
          id: 19,
        },
        {
          name: 'Pink',
          id: 26,
        },
        {
          name: 'Yellow',
          id: 20,
        },
      ],
    },
  },
];

I am trying to create a new array of data2 with only the "Color" options present in data1 objects.我正在尝试创建一个新的 data2 数组,其中只有 data1 对象中存在的“颜色”选项。 For this example above, I want to return a new array that looks like this:对于上面的示例,我想返回一个如下所示的新数组:

const data2 = [
  {
    invitation: {
      name: 'Type',
      options: [
        {
          name: 'Show1',
          id: 21,
        },

        {
          name: 'Show2',
          id: 22,
        },
      ],
    },
  },

  {
    color: {
      name: 'Color',
      options: [
        {
          name: 'Gray',
          id: 19,
        },
       {
          name: 'Yellow',
          id: 20,
        },
      ],
    },
  },
];

Here is my code so far到目前为止,这是我的代码

const obj = data1.reduce((acc, item) => {
  acc[item.id] = item;
  return acc;
})

data2.forEach(d => {

return d;

});

console.log(data2);

caching one of the arrays in an object, it reduces the run time to linear.在 object 中缓存 arrays 之一,它将运行时间减少到线性。 Then I am using a forEach method.然后我正在使用 forEach 方法。 I am just not sure where to go from here.我只是不确定 go 从这里到哪里。

This should solve the problem for you !这应该可以为您解决问题!

Created a set of unique color ids and then filter the data of colors from data2 based on the color ids present in the set创建了一组唯一的颜色 id,然后根据集合中存在的颜色 id 从 data2 中过滤 colors 的数据

 const data1 = [ { name: 'I am obj one', id: 125, category: [18], colors: [19, 20], }, { name: 'I am obj two', id: 111, category: [18], colors: [19, 20], }, ]; const data2 = [ { invitation: { name: 'Type', options: [ { name: 'Show1', id: 21, }, { name: 'Show2', id: 22, }, ], }, }, { color: { name: 'Color', options: [ { name: 'Gray', id: 19, }, { name: 'Pink', id: 26, }, { name: 'Yellow', id: 20, }, ], }, }, ]; var set = new Set(); data1.forEach(obj => { obj.colors.forEach(color => set.add(color));} ) data2[1].color.options = data2[1].color.options.filter(option => set.has(option.id)); console.log(data2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM