如何减少Javascript中多个变量的对象数组?

如何减少Javascript中多个变量的对象数组?

问题描述:

我想知道如何根据几个属性减少大量的对象。阵列看起来像:如何减少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"}] 

所得降低对象的阵列需要包括从每一个district对于给定的犯罪type集合体count在任何给定日期(to_timestamp);日期是每周。喜欢的东西:

[{key: "MOTOR VEHICLE THEFT", value: 57, date:"2015/09/23"}, 
... 
    {key: "ASSAULT", value: 77, date:"2016/03/23"}] 

我已经使用日期转换的时刻。

您可以使用一个对象作为散列表,用于通缉组(typeto_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);

+0

不能说我完全理解它(还),但它肯定不会工作。马洛。你能解释一下线上发生的事情: 'var key = groupBy.map(function(k){return a [k];})。join('|');' – Kwhitejr

+0

这行建立一个具有属性值的新密钥,在'groupBy'中定义。例如,对于第一个元素,您可以为数组中的第一个属性'type'赋值'“MOTOR VEHICLE THEFT”'和'to_timestamp'值''2015-09-24T00:00:00.000Z“'。然后用'|'连接,结果为''MOTOR VEHICLE THEFT | 2015-09-24T00:00:00.000Z“'。这个结果被用作对象的散列。 –

我认为你可以用一个简单的Array.prototype.forEach循环做到这一点:

var crimes = [ 
 
\t {count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, 
 
\t {count:6, district:12, to_timestamp:"2015-09-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, 
 
\t {count:14, district:19, to_timestamp:"2015-10-01T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, 
 
\t {count:4, district:19, to_timestamp:"2015-09-24T00:00:00.000Z", type:"VANDALISM"}, 
 
\t {count:4, district:19, to_timestamp:"2016-03-24T00:00:00.000Z", type:"MOTOR VEHICLE THEFT"}, 
 
\t {count:7, district:10, to_timestamp:"2016-03-24T00:00:00.000Z", type:"ASSAULT"} 
 
]; 
 

 
var dict = {}, 
 
\t result = []; 
 
crimes.forEach(crime => { 
 
\t var date = new Date(crime.to_timestamp); 
 
\t date.setDate(date.getDate() - date.getDay()); 
 
\t var hash = crime.type + date.toDateString() 
 
\t if (!dict[hash]) { 
 
\t \t dict[hash] = { 
 
\t \t \t count: crime.count, 
 
\t \t \t key: crime.type, 
 
\t \t \t date: date 
 
\t \t }; 
 
\t \t result.push(dict[hash]) 
 
\t } else 
 
\t \t dict[hash].count += crime.count; 
 
}); 
 

 
console.log(result);

,但我认为你减少一个对象会更容易更然后一个数组。在获得对象后,可以将其转换为数组

此代码仅将datatype合并到对象关键字。没有添加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>

+0

谢谢你的减少答案! – Kwhitejr