2016-07-08 98 views
1

我想知道如何根據幾個屬性減少大量的對象。陣列看起來像:如何減少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"}] 

我已經使用日期轉換的時刻。

回答

4

您可以使用一個對象作爲散列表,用於通緝組(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「'。這個結果被用作對象的散列。 –

0

我認爲你可以用一個簡單的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);

1

,但我認爲你減少一個對象會更容易更然後一個數組。在獲得對象後,可以將其轉換爲數組

此代碼僅將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