[英]How to read values and sum over a dynamic array?
I have an JSON like this 我有这样的JSON
"result": [{
"channel": "A",
"mkp": "ABC",
"qtd": 6,
"total": 2938.2,
"data": "2019-02-16",
"time": "22:30:40"
}, {
"channel": "C",
"mkp": "DEF",
"qtd": 1545,
"total": 2127229.64,
"data": "2019-02-20",
"time": "17:19:49"
}, {
"channel": "C",
"mkp": "JKL",
"qtd": 976,
"total": 1307328.37,
"data": "2019-02-20",
"time": "17:19:53"
}, {
"channel": "U",
"mkp": "PQR",
"qtd": 77,
"total": 98789.87,
"data": "2019-02-20",
"time": "16:12:31"
}, {
"channel": "U",
"mkp": "STU",
"qtd": 427,
"total": 433206.62,
"data": "2019-02-20",
"time": "17:04:27"
}
]
I need to sum the QTD, the total and return the newest data + time when the channel is the same (eg.: Channel C and U have 2 entries), if it's not so I only will display the values, but I can't figure it out how could I iterate and do these math. 我需要对QTD进行总计,并在通道相同时返回最新数据+时间(例如:通道C和U有2个条目),如果不是,那么我只会显示值,但是我可以不能弄清楚我该如何迭代并进行这些数学运算。 Someone could help? 有人可以帮忙吗?
A sample of what I want: 我想要的样本:
"A": [{
"qtd": 6,
"total": 2938.20,
"dateTime": 2019 - 02 - 16 22: 30: 40 "
}],
"C": [{
"qtd": 2.521,
"total": 3434558.01,
"dateTime": 2019 - 02 - 20 17: 19: 53 "
}],
"U": [{
"qtd": 504,
"total": 531996,
49,
"dateTime": 2019 - 02 - 20 17: 04: 27 "
}]
Currently I separated the values using filter
like this: 目前,我使用filter
将值分开,如下所示:
this.channelA = this.receivedJson.filter(({ channel }) => channel === "A");
You could use reduce
method and return one object with object as values. 您可以使用reduce
方法,并返回一个对象,其中对象作为值。
const data = [{"channel":"A","mkp":"ABC","qtd":6,"total":2938.2,"data":"2019-02-16","time":"22:30:40"},{"channel":"C","mkp":"DEF","qtd":1545,"total":2127229.64,"data":"2019-02-20","time":"17:19:49"},{"channel":"C","mkp":"JKL","qtd":976,"total":1307328.37,"data":"2019-02-20","time":"17:19:53"},{"channel":"U","mkp":"PQR","qtd":77,"total":98789.87,"data":"2019-02-20","time":"16:12:31"},{"channel":"U","mkp":"STU","qtd":427,"total":433206.62,"data":"2019-02-20","time":"17:04:27"}] const res = data.reduce((r, {channel, qtd, total, data, time}) => { const dateTime = `${data} ${time}` if(!r[channel]) r[channel] = {qtd, total, dateTime} else { r[channel].total += total r[channel].qtd += qtd; r[channel].dateTime = dateTime } return r; }, {}) console.log(res)
You an use reduce
to group the values based on channel
like this: 您可以使用reduce
来基于channel
将值分组,如下所示:
const input = [{"channel":"A","mkp":"ABC","qtd":6,"total":2938.2,"data":"2019-02-16","time":"22:30:40"},{"channel":"C","mkp":"DEF","qtd":1545,"total":2127229.64,"data":"2019-02-20","time":"17:19:49"},{"channel":"C","mkp":"JKL","qtd":976,"total":1307328.37,"data":"2019-02-20","time":"17:19:53"},{"channel":"U","mkp":"PQR","qtd":77,"total":98789.87,"data":"2019-02-20","time":"16:12:31"},{"channel":"U","mkp":"STU","qtd":427,"total":433206.62,"data":"2019-02-20","time":"17:04:27"}] const merged = input.reduce((acc, { channel, qtd, total, data, time }) => { acc[channel] = acc[channel] || [{ qtd: 0, total: 0, dateTime:'' }]; const group = acc[channel][0]; group.qtd += qtd; group.total += total; const dateTime = `${data} ${time}` if(dateTime > group.dateTime) group.dateTime = dateTime; return acc; }, {}) console.log(merged)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.