提问者:小点点

通过匹配来自另一个数组的值对数组进行分组


我试图通过从另一个数组列表中筛选数组来重构它。

包含筛选器的数组:

const filteringTags: [
    'Kitchen',
    'Accessories',
    ...
]

我要操作的数组:

[
    {
        category: 'free',
        date: '2020-04-04',
        title: 'Some Title',
        tags: [
            'Kitchen',
            'Accessories'
        ]
    },
    {
        category: 'premium',
        date: '2020-04-05',
        title: 'Another Title',
        tags: [
            'Kitchen'
        ]
    },
    ...
]

预期结果:

[
    'Kitchen': [
        {
            category: 'free',
            date: '2020-04-04',
            title: 'Some Title',
            tags: [
                'Kitchen',
                'Accessories'
            ]
        },
        {
            category: 'premium',
            date: '2020-04-05',
            title: 'Another Title',
            tags: [
                'Kitchen'
            ]
        }
    ],
    'Accessories': [
        {
            category: 'free',
            date: '2020-04-04',
            title: 'Some Title',
            tags: [
                'Kitchen',
                'Accessories'
            ]
        }
    ]
]

我试过的:不幸的是,这里没有什么有用的东西可以张贴。

我尽量避免使用库,所以如果您对使用ES6有任何建议,我将非常感谢。


共3个答案

匿名用户

将所需输出视为对象数组,

null

const data = [
  {
    category: 'free',
    date: '2020-04-04',
    title: 'Some Title',
    tags: ['Kitchen', 'Accessories'],
  },
  {
    category: 'premium',
    date: '2020-04-05',
    title: 'Another Title',
    tags: ['Kitchen'],
  },
];

const filteringTags = ['Kitchen', 'Accessories'];

const getGroupedData = (data, filters) => {
  const result = {};
  filters.forEach(filter => {
    result[filter] = [
      ...(result[filter] || ''),
      ...data.filter(d => d.tags.includes(filter)),
    ];
  });
  return result;
};

let finalResult = getGroupedData(data, filteringTags);

finalResult = Object.keys(finalResult).map(key => ({
  [key]: finalResult[key],
}));

console.log(finalResult);

匿名用户

下面是一个完整的工作示例,它产生了所需的结果:

null

const filteringTags = [
    'Kitchen',
    'Accessories',
];

const data = [
    {
        category: 'free',
        date: '2020-04-04',
        title: 'Some Title',
        tags: [
            'Kitchen',
            'Accessories'
        ]
    },
    {
        category: 'premium',
        date: '2020-04-05',
        title: 'Another Title',
        tags: [
            'Kitchen'
        ]
    },
];

function byTag(tags, data) {
  let tagMap = {};
  for (let tag of tags) {
    tagMap[tag] = [];
    for (let datum of data) {
       if (datum.tags.includes(tag)) {
          tagMap[tag].push(datum);
       }
    }
  }
  return tagMap;
}

let result = byTag(filteringTags, data);
console.log(JSON.stringify(result, null, 2));

匿名用户

简短而漂亮的解决你的任务

const filteringTags = [
  'Kitchen',
  'Accessories',
] 

const arr = [
  {
    category: 'free',
    date: '2020-04-04',
    title: 'Some Title',
    tags: [
        'Kitchen',
        'Accessories'
    ]
},
{
    category: 'premium',
    date: '2020-04-05',
    title: 'Another Title',
    tags: [
        'Kitchen'
    ]
}
]

const resultArr = filteringTags.map((it, index) => {
  return { [it]: arr }
})

如果需要对象,请使用此函数

const resultObj = filteringTags.reduce((acc, it) => {
  return { ...acc, [it]: arr }
}, {})