[英]How to reduce an array of objects on multiple variables in Javascript?
我想知道如何基於幾個屬性來減少大量對象。 該數組如下所示:
[{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"}]
結果減少的對象數組需要包括任何給定日期( to_timestamp
)上給定犯罪type
每個district
的合計count
; 日期是每周。 就像是:
[{key: "MOTOR VEHICLE THEFT", value: 57, date:"2015/09/23"},
...
{key: "ASSAULT", value: 77, date:"2016/03/23"}]
我已經在使用Moment進行日期轉換了。
您可以將一個對象用作所需組的哈希表( 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);
但是我認為減少對象比數組更容易。 獲得對象后,可以將其轉換為數組
此代碼只是將data
和type
到對象鍵中。 沒有加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>
我認為您可以使用一個簡單的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.