[英]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.