[英]How to reduce an array of objects on multiple variables in Javascript?
I would like to know how to reduce a large array of objects based upon several properties. 我想知道如何基于几个属性来减少大量对象。 The array looks like:
该数组如下所示:
[{count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:6, district:12, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:14, district:19, to_timestamp:"2015-10-01T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"VANDALISM"},
...
{count:4, district:19, to_timestamp:"2016-03-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"},
{count:7, district:10, to_timestamp:"2016-03-24T00:00:00.000Z", type:"ASSAULT"}]
Resulting reduced array of objects needs to include the aggregate count
from every district
for given crime type
on any given date ( to_timestamp
); 结果减少的对象数组需要包括任何给定日期(
to_timestamp
)上给定犯罪type
每个district
的合计count
; dates are weekly. 日期是每周。 Something like:
就像是:
[{key: "MOTOR VEHICLE THEFT", value: 57, date:"2015/09/23"},
...
{key: "ASSAULT", value: 77, date:"2016/03/23"}]
I'm already using Moment for date conversions. 我已经在使用Moment进行日期转换了。
You could use an object as hash table for the wanted groups ( type
and to_timestamp
) and use Array#forEach
for iterating the array. 您可以将一个对象用作所需组的哈希表(
type
和to_timestamp
),并使用Array#forEach
迭代该数组。
var data = [{ count: 4, district: 19, to_timestamp: "2015-09-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 6, district: 12, to_timestamp: "2015-09-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 14, district: 19, to_timestamp: "2015-10-01T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 4, district: 19, to_timestamp: "2015-09-24T00:00:00.000Z", type: "VANDALISM" }, { count: 4, district: 19, to_timestamp: "2016-03-24T00:00:00.000Z", type: "MOTOR VEHICLE THEFT" }, { count: 7, district: 10, to_timestamp: "2016-03-24T00:00:00.000Z", type: "ASSAULT" }], groupBy = ['type', 'to_timestamp'], grouped = []; data.forEach(function (a) { var key = groupBy.map(function (k) { return a[k]; }).join('|'); if (!this[key]) { this[key] = { key: a.type, value: 0, date: a.to_timestamp.slice(0, 10).replace(/-/g, '/') }; grouped.push(this[key]); } this[key].value += a.count; }, Object.create(null)); console.log(grouped);
but I think you reduce a object will be easier more then a array. 但是我认为减少对象比数组更容易。 after you got a object, you can convert it to a array
获得对象后,可以将其转换为数组
This code are just combine the data
and type
to the object key. 此代码只是将
data
和type
到对象键中。 Did not add district
. 没有加
district
。
const object = [{count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:6, district:12, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:14, district:19, to_timestamp:"2015-10-01T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"VANDALISM"}, {count:4, district:19, to_timestamp:"2016-03-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:7, district:10, to_timestamp:"2016-03-24T00:00:00.000Z", type:"ASSAULT"}] // reduce a object .reduce( ( res, item) => { const date = moment(item.to_timestamp).format('YYYY/MM/DD') const key = item.type + date if ( !res[key] ) { res[key] = {key: item.type, date: date, value: item.count} } else { res[key]['value'] += item.count } return res }, {} ) //get a object, and then the object to array //console.log(object) var result = [] for ( var key in object ) { result.push(object[key]) } console.log(result)
<script src="http://momentjs.com/downloads/moment.min.js"></script>
I think you can do this with a simple Array.prototype.forEach
loop: 我认为您可以使用一个简单的
Array.prototype.forEach
循环来做到这一点:
var crimes = [ {count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:6, district:12, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:14, district:19, to_timestamp:"2015-10-01T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"VANDALISM"}, {count:4, district:19, to_timestamp:"2016-03-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, {count:7, district:10, to_timestamp:"2016-03-24T00:00:00.000Z", type:"ASSAULT"} ]; var dict = {}, result = []; crimes.forEach(crime => { var date = new Date(crime.to_timestamp); date.setDate(date.getDate() - date.getDay()); var hash = crime.type + date.toDateString() if (!dict[hash]) { dict[hash] = { count: crime.count, key: crime.type, date: date }; result.push(dict[hash]) } else dict[hash].count += crime.count; }); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.