[英]How to group array objects by one of their fields?
我正在尝试通过从另一个数组列表中过滤来重组数组。
包含过滤器的数组:
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'
]
}
]
]
我试图避免使用库,所以如果您有任何建议,如果您使用 vanilla JS,我将不胜感激。
考虑到所需的 output 作为对象数组,
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] = [...JSON.parse(JSON.stringify((result[filter] || ''))), ...JSON.parse(JSON.stringify(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);
将 output 视为 object
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] = [...JSON.parse(JSON.stringify((result[filter] || ''))), ...JSON.parse(JSON.stringify(data.filter(d => d.tags.includes(filter)))), ]; }); return result; }; let finalResult = getGroupedData(data, filteringTags); console.log(finalResult);
希望这可以帮助。
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 }
})
如果需要object,那么就用这个function
const resultObj = filteringTags.reduce((acc, it) => {
return { ...acc, [it]: arr }
}, {})
这是一个完整的工作示例,可产生所需的结果:
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));
你可以这样做:
var arr = [ { category: 'free', date: '2020-04-04', title: 'Some Title', tags: [ 'Kitchen', 'Accessories' ] }, { category: 'premium', date: '2020-04-05', title: 'Another Title', tags: [ 'Kitchen' ] }, ]; var res = arr.reduce((acc, elem)=>{ elem.tags.forEach(k=>{ acc[k] = acc[k]? [...acc[k], JSON.parse(JSON.stringify(elem))]: [JSON.parse(JSON.stringify(elem))]; }) return acc },{}); console.log(res)
你可以这样做:
reduce()
filter()
var filteringTags= [ 'Kitchen', 'Accessories']; var arr=[ { category: 'free', date: '2020-04-04', title: 'Some Title', tags: [ 'Kitchen', 'Accessories' ] }, { category: 'premium', date: '2020-04-05', title: 'Another Title', tags: [ 'Kitchen' ] }]; var result = filteringTags.reduce((acc, e)=>{ acc[e] = arr.filter(k=>k.tags.includes(e)); return acc; },{}); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.