如何在JavaScript中按数组的一个对象键对数组进行分组
如何在angular中使用groupBy对对象进行分组如何在JavaScript中按数组的一个对象键对数组进行分组,javascript,Javascript,如何在angular中使用groupBy对对象进行分组 rowData = [ {code: "Machine 1", date: "2019-01-19 02:00:00", assets: { PRN: 2}}, {code: "Machine 2", date: "2019-01-20 00:00:00", assets: { PRN1: 2}}, {code: "Machine 3", date: "2019-01-21 00:00:00", assets: { PRN: 2
rowData = [
{code: "Machine 1", date: "2019-01-19 02:00:00", assets: { PRN: 2}},
{code: "Machine 2", date: "2019-01-20 00:00:00", assets: { PRN1: 2}},
{code: "Machine 3", date: "2019-01-21 00:00:00", assets: { PRN: 2}},
{code: "Machine 4", date: "2019-01-22 00:00:00", assets: { PRN1: 2}},
{code: "Machine 5", date: "2019-01-23 00:00:00", assets: { PRN2: 2}}
]
将其分组为对象键,即PRN
,PRN1
,PRN2
输出应该是
PRN: [{...}]
PRN1: [{...}]
PRN2: [{...}]
您可以将RxJS与angular一起使用,并通过以下方式进行操作: 参考:
您可以将RxJS与angular一起使用,并通过以下方式进行操作: 参考:
您不需要使用RxJS,因为您可以通过内置的
数组来实现
rowData.reduce((groupedBy, row) => {
// get the PRN/PRN1/PRN2 value
const key = Object.keys(row.assets)[0];
// create an array of rows belonging to the key
// if it does not already exist
if (!Array.isArray(groupedBy[key])) {
groupedBy[key] = [];
}
// add the current row to the corresponding object key
groupedBy[key].push(row);
return groupedBy;
}, {})
结果:
{
"PRN": [
{
"code": "Machine 1",
"date": "2019-01-19 02:00:00",
"assets": {
"PRN": 2
}
},
{
"code": "Machine 3",
"date": "2019-01-21 00:00:00",
"assets": {
"PRN": 2
}
}
],
"PRN1": [
{
"code": "Machine 2",
"date": "2019-01-20 00:00:00",
"assets": {
"PRN1": 2
}
},
{
"code": "Machine 4",
"date": "2019-01-22 00:00:00",
"assets": {
"PRN1": 2
}
}
],
"PRN2": [
{
"code": "Machine 5",
"date": "2019-01-23 00:00:00",
"assets": {
"PRN2": 2
}
}
]
}
您不需要使用RxJS,因为您可以通过内置的数组来实现
rowData.reduce((groupedBy, row) => {
// get the PRN/PRN1/PRN2 value
const key = Object.keys(row.assets)[0];
// create an array of rows belonging to the key
// if it does not already exist
if (!Array.isArray(groupedBy[key])) {
groupedBy[key] = [];
}
// add the current row to the corresponding object key
groupedBy[key].push(row);
return groupedBy;
}, {})
结果:
{
"PRN": [
{
"code": "Machine 1",
"date": "2019-01-19 02:00:00",
"assets": {
"PRN": 2
}
},
{
"code": "Machine 3",
"date": "2019-01-21 00:00:00",
"assets": {
"PRN": 2
}
}
],
"PRN1": [
{
"code": "Machine 2",
"date": "2019-01-20 00:00:00",
"assets": {
"PRN1": 2
}
},
{
"code": "Machine 4",
"date": "2019-01-22 00:00:00",
"assets": {
"PRN1": 2
}
}
],
"PRN2": [
{
"code": "Machine 5",
"date": "2019-01-23 00:00:00",
"assets": {
"PRN2": 2
}
}
]
}
您可以使用Array.prototype.reduce
函数实现
rowData.reduce((groupedBy, row) => {
// get the PRN/PRN1/PRN2 value
const key = Object.keys(row.assets)[0];
// create an array of rows belonging to the key
// if it does not already exist
if (!Array.isArray(groupedBy[key])) {
groupedBy[key] = [];
}
// add the current row to the corresponding object key
groupedBy[key].push(row);
return groupedBy;
}, {})
让行数据=[
{代码:“机器1”,日期:“2019-01-19 02:00:00”,资产:{PRN:2},
{代码:“机器2”,日期:“2019-01-20 00:00:00”,资产:{PRN1:2},
{代码:“机器3”,日期:“2019-01-21 00:00:00”,资产:{PRN:2},
{代码:“机器4”,日期:“2019-01-22 00:00:00”,资产:{PRN1:2},
{代码:“机器5”,日期:“2019-01-23 00:00:00”,资产:{PRN2:2}
]
设ans=rowData.reduce((acc,val)=>{
让key=Object.keys(val.assets)[0];
acc[键]=acc[键]?acc[键]:[];
acc[键]。推送(val);
返回acc;
}, {})
控制台日志(ans)代码>您可以实现的是使用数组。原型。reduce
函数
rowData.reduce((groupedBy, row) => {
// get the PRN/PRN1/PRN2 value
const key = Object.keys(row.assets)[0];
// create an array of rows belonging to the key
// if it does not already exist
if (!Array.isArray(groupedBy[key])) {
groupedBy[key] = [];
}
// add the current row to the corresponding object key
groupedBy[key].push(row);
return groupedBy;
}, {})
让行数据=[
{代码:“机器1”,日期:“2019-01-19 02:00:00”,资产:{PRN:2},
{代码:“机器2”,日期:“2019-01-20 00:00:00”,资产:{PRN1:2},
{代码:“机器3”,日期:“2019-01-21 00:00:00”,资产:{PRN:2},
{代码:“机器4”,日期:“2019-01-22 00:00:00”,资产:{PRN1:2},
{代码:“机器5”,日期:“2019-01-23 00:00:00”,资产:{PRN2:2}
]
设ans=rowData.reduce((acc,val)=>{
让key=Object.keys(val.assets)[0];
acc[键]=acc[键]?acc[键]:[];
acc[键]。推送(val);
返回acc;
}, {})
控制台日志(ans)
AngularJs中没有groupBy
,只有AngularJs中没有,这与您问题中的标记无关。这可以在JavaScript/TypeScript中解决,与Angular无关。您可以使用array#reduce
对数据进行分组。对于AngularJs,Angular2+可以使用,或者AngularJs中没有groupBy
,只有AngularJs,这不是您问题中的标记所涉及的内容。这可以通过JavaScript/TypeScript解决,并且与Angular无关。您可以使用array#reduce
对数据进行分组。对于Angular 2+,可以使用或如何使其成为数组?例如[0:{PRN:{..},1{PRN1:{..},2{PRN2:{..}]如何使其成为数组?例如[0:{PRN:{..},1{PRN1{..},2{PRN2:{..}]