我知道,关于对象数组中的一个属性进行分组的问题不计其数。但是我想做的是更具体一些:
const lists = [
{ groupKey: 'ABC', key: 'r8', timestamp: '2014', index: 2 },
{ groupKey: 'ABC', key: 'q8', timestamp: '2012', index: 0 },
{ groupKey: 'ABC', key: 'w8', timestamp: '2013', index: 1 },
{ groupKey: 'CDE', key: 'r7', timestamp: '2019', index: 0 }
]
结果应按groupKey分组,并按索引排序
(这里的索引是迭代器0、1、2、3 ...,因此无需实际排序,而是按数组的正确顺序放置。例如:array [index] = ...)。
这应该看起来像:
{
ABC: [
{ key: 'q8', timestamp: '2012', index: 0 },
{ key: 'w8', timestamp: '2013', index: 1 },
{ key: 'r8', timestamp: '2014', index: 2 }
],
CDE: [
{ key: 'r7', timestamp: '2019', index: 0 }
]
}
我尝试不进行排序就进行分组:
const result = lists.reduce((r, item) => {
let { groupKey, ...rest } = item
r[item.groupKey] = [...(r[item.groupKey] || []), rest]
return r
}, {})
通过排序,并不成功,但是您知道我的意思:
const result = lists.reduce((r, item) => {
let { groupKey, ...rest } = item
r[item.groupKey][item.index] = rest //err: can not set property 2 of undefined
return r
}, {})
任何建议都值得赞赏
答案 0 :(得分:3)
您可以直接获取索引,而无需稍后排序。
const
lists = [{ groupKey: 'ABC', key: 'r8', timestamp: '2014', index: 2 }, { groupKey: 'ABC', key: 'q8', timestamp: '2012', index: 0 }, { groupKey: 'ABC', key: 'w8', timestamp: '2013', index: 1 }, { groupKey: 'CDE', key: 'r7', timestamp: '2019', index: 0 }],
result = lists.reduce((r, { groupKey, ...rest }) => {
r[groupKey] = r[groupKey] || [];
r[groupKey][rest.index] = rest;
return r
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
尝试这样
const lists = [
{ groupKey: 'ABC', key: 'r8', timestamp: '2014', index: 2 },
{ groupKey: 'ABC', key: 'q8', timestamp: '2012', index: 0 },
{ groupKey: 'ABC', key: 'w8', timestamp: '2013', index: 1 },
{ groupKey: 'CDE', key: 'r7', timestamp: '2019', index: 0 }
]
var groupBy = function(datas, key) {
return datas.reduce(function(data, x) {
var dKey = x[key];
delete x[key];
(data[dKey] = data[dKey] || []).push(x);
return data;
}, {});
};
console.log(groupBy(lists, 'groupKey'));
答案 2 :(得分:0)
您快到了,您只需要在reduce
完成后对 进行排序:
const lists = [{
groupKey: 'ABC',
key: 'r8',
timestamp: '2014',
index: 2
},
{
groupKey: 'ABC',
key: 'q8',
timestamp: '2012',
index: 0
},
{
groupKey: 'ABC',
key: 'w8',
timestamp: '2013',
index: 1
},
{
groupKey: 'CDE',
key: 'r7',
timestamp: '2019',
index: 0
}
]
const result = lists.reduce((r, item) => {
const { groupKey, ...rest } = item
r[groupKey] = [...(r[groupKey] || []), rest];
return r;
}, {})
Object.values(result).forEach((arr) => {
arr.sort((a, b) => a.index - b.index);
});
console.log(result);
答案 3 :(得分:0)
首先可以按键分组,然后需要根据索引进行排序
const lists = [{ groupKey: 'ABC', key: 'r8', timestamp: '2014', index: 2 },{ groupKey: 'ABC', key: 'q8', timestamp: '2012', index: 0 }, { groupKey: 'ABC', key: 'w8', timestamp: '2013', index: 1 },{ groupKey: 'CDE', key: 'r7', timestamp: '2019', index: 0 } ]
// group by groupkey
let grouped = lists.reduce((op, inp) => {
let groupKey = inp.groupKey
op[groupKey] = op[groupKey] || []
op[groupKey].push(inp)
return op
},{})
// sort by index
let output = Object.entries(grouped).reduce((op,[key,value]) => {
value = value.sort((a,b)=> a.index-b.index)
op[key] = value
return op
},{})
console.log(output)
答案 4 :(得分:0)
尝试一下:
const result = lists.reduce((r, item) => {
let { groupKey, ...rest } = item
r[item.groupKey] = [...(r[item.groupKey] || []), rest];
(r[item.groupKey]).sort((a, b) => a.index - b.index);
return r
}, {});