Javascript - 按对象数组中的一个 属性 个对象分组
Javascript - Group by one property of object in an array of objects
我知道,关于对象数组中的一个 属性 有无数的问题。但我想做的是更具体一点:
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
}, {})
如有任何建议,我们将不胜感激
这样试试
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'));
快完成了,您只需要在 之后对 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);
可以先按key分组,再按index排序
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)
可以直接取索引,不用后面排序
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; }
试试这个:
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
}, {});
我知道,关于对象数组中的一个 属性 有无数的问题。但我想做的是更具体一点:
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
}, {})
如有任何建议,我们将不胜感激
这样试试
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'));
快完成了,您只需要在 之后对 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);
可以先按key分组,再按index排序
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)
可以直接取索引,不用后面排序
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; }
试试这个:
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
}, {});