如何从 object 的数组中获取特定键值的总和
How to get sum of specific keys values from array of object
我有类似于下面的 objects 数组:
let violation = [
{
"timestamp": 1637658787661,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637658789678,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637659776571,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateIdlingEvent",
"value": 0,
},
{
"timestamp": 1637660707375,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 67,
},
{
"timestamp": 1637661519707,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637661521773,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637661548282,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 62,
},
{
"timestamp": 1637663230199,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 66,
},
.....
.....
]
所以在这里,每个 object 都会有一个 message
属性 或四个值之一的键,即 CreateHarshAccelerationEvent
、CreateHarshDecelerationEvent
、CreateSpeedingEvent
, CreateIdlingEvent
.
这里我需要得到每个key的总数。即总计 CreateHarshAccelerationEvent
总计 CreateHarshDecelerationEvent
总计 CreateIdlingEvent
总计 CreateSpeedingEvent
但条件是我必须 select object 与标识符合计,而该标识符是 driver 和公司 ID 的组合,即
key = company + "/" + driver
我正在为此做以下代码
var violations = []
for (let violation of data){
var key
if ( (violation.company) && (violation.driver) ) {
key = violation.company + "/" + violation.driver;
let violationType = violation.message;
violations[key].company = violation.company;
violations[key].driver = violation.driver;
}
}
console.log(violations);
我需要结果为
[{
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"CreateHarshAccelerationEvent": "total number of values CreateHarshAccelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateHarshDecelerationEvent": "total number of values CreateHarshDecelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateIdlingEvent": "total number of values CreateIdlingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateSpeedingEvent": "total number of values CreateSpeedingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
}]
您的方法有问题。
您不能将字符串用作数组中的键,因此您需要将其更改为对象
const violations = {}
也在你做之前
violations[key].speeding++
确保初始化为 0,因为你试图在此处增加 undefined
值
您可以使用 array#reduce
总结相同 driver
和 company
的不同事件。
let violation = [ { "timestamp": 1637658787661, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637658789678, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637659776571, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateIdlingEvent", "value": 0, }, { "timestamp": 1637660707375, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 67, }, { "timestamp": 1637661519707, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637661521773, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5","message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637661548282, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 62, }, { "timestamp":1637663230199, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 66, }],
result = Object.values(violation.reduce((r, o) => {
const key = o.driver + '_' + o.company;
r[key] ??= {driver: o.driver, company: o.company};
r[key][o.message] = (r[key][o.message] ?? 0) + o.value;
return r;
},{}));
console.log(result);
Array.reduce
会帮助你。
let violation = [ { "timestamp": 1637658787661, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637658789678, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637659776571, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateIdlingEvent", "value": 0, }, { "timestamp": 1637660707375, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 67, }, { "timestamp": 1637661519707, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637661521773, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5","message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637661548282, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 62, }, { "timestamp":1637663230199, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 66, }];
const result = violation.reduce((acc, curr) => {
const node = acc.find(item => item.driver === curr.driver && item.company === curr.company);
if (node) node[curr.message] ? node[curr.message] += curr.value : node[curr.message] = curr.value
else
acc.push({
driver: curr.driver,
company: curr.company,
[curr.message]: curr.value
});
return acc;
}, []);
console.log(result);
我有类似于下面的 objects 数组:
let violation = [
{
"timestamp": 1637658787661,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637658789678,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637659776571,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateIdlingEvent",
"value": 0,
},
{
"timestamp": 1637660707375,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 67,
},
{
"timestamp": 1637661519707,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshAccelerationEvent",
"value": 71,
},
{
"timestamp": 1637661521773,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateHarshDecelerationEvent",
"value": 50,
},
{
"timestamp": 1637661548282,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 62,
},
{
"timestamp": 1637663230199,
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"message": "CreateSpeedingEvent",
"value": 66,
},
.....
.....
]
所以在这里,每个 object 都会有一个 message
属性 或四个值之一的键,即 CreateHarshAccelerationEvent
、CreateHarshDecelerationEvent
、CreateSpeedingEvent
, CreateIdlingEvent
.
这里我需要得到每个key的总数。即总计 CreateHarshAccelerationEvent
总计 CreateHarshDecelerationEvent
总计 CreateIdlingEvent
总计 CreateSpeedingEvent
但条件是我必须 select object 与标识符合计,而该标识符是 driver 和公司 ID 的组合,即
key = company + "/" + driver
我正在为此做以下代码
var violations = []
for (let violation of data){
var key
if ( (violation.company) && (violation.driver) ) {
key = violation.company + "/" + violation.driver;
let violationType = violation.message;
violations[key].company = violation.company;
violations[key].driver = violation.driver;
}
}
console.log(violations);
我需要结果为
[{
"driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7",
"company": "9d8616dd-4689-4689-8812-a2345ccdcfc5",
"CreateHarshAccelerationEvent": "total number of values CreateHarshAccelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateHarshDecelerationEvent": "total number of values CreateHarshDecelerationEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateIdlingEvent": "total number of values CreateIdlingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
"CreateSpeedingEvent": "total number of values CreateSpeedingEvent from Array of objects basis on the basis of that custom key i.e. company/driver value",
}]
您的方法有问题。
您不能将字符串用作数组中的键,因此您需要将其更改为对象
const violations = {}
也在你做之前
violations[key].speeding++
确保初始化为 0,因为你试图在此处增加 undefined
值
您可以使用 array#reduce
总结相同 driver
和 company
的不同事件。
let violation = [ { "timestamp": 1637658787661, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637658789678, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637659776571, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateIdlingEvent", "value": 0, }, { "timestamp": 1637660707375, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 67, }, { "timestamp": 1637661519707, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637661521773, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5","message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637661548282, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 62, }, { "timestamp":1637663230199, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 66, }],
result = Object.values(violation.reduce((r, o) => {
const key = o.driver + '_' + o.company;
r[key] ??= {driver: o.driver, company: o.company};
r[key][o.message] = (r[key][o.message] ?? 0) + o.value;
return r;
},{}));
console.log(result);
Array.reduce
会帮助你。
let violation = [ { "timestamp": 1637658787661, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637658789678, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637659776571, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateIdlingEvent", "value": 0, }, { "timestamp": 1637660707375, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 67, }, { "timestamp": 1637661519707, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateHarshAccelerationEvent", "value": 71, }, { "timestamp": 1637661521773, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5","message": "CreateHarshDecelerationEvent", "value": 50, }, { "timestamp": 1637661548282, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 62, }, { "timestamp":1637663230199, "driver": "7f5fda5d-86a8-471e-ae32-fcc39deb8fc7", "company": "9d8616dd-4689-4689-8812-a2345ccdcfc5", "message": "CreateSpeedingEvent", "value": 66, }];
const result = violation.reduce((acc, curr) => {
const node = acc.find(item => item.driver === curr.driver && item.company === curr.company);
if (node) node[curr.message] ? node[curr.message] += curr.value : node[curr.message] = curr.value
else
acc.push({
driver: curr.driver,
company: curr.company,
[curr.message]: curr.value
});
return acc;
}, []);
console.log(result);