这是我第一次在这里发布消息,因此,如果缺少任何内容或没有太多信息,请告诉我。我在旅途中学习。
我从API提取了一些数据,现在我需要整理所有数据,因此最终我将只保留键和每个键的总值。
我需要每个Version=1.0.12205.0
求和每个区域内storeArea
和boysTotal
的所有值,所以看起来像这样:girlsTotal
,{{1} }
我首先映射[area1
并为每个区域提出了3个不同的对象,然后尝试将所有3193745.7189541734]
和json
合并为一个数组,这样我就可以将一个数组的总数相加具有减少功能,但我肯定错过了一些重要的事情。它是什么?感谢您的帮助!
boysTotal
答案 0 :(得分:2)
使用map
和reduce
是可行的方法,但是您必须map
进入stores
,reduce
进入totalStore
:< / p>
const json={"stores":[{"storeArea":"area1","totalStore":[{"date":"2018-10-01","boysTotal":1082220.3945979946,"girlsTotal":9585.8794674401543},{"date":"2018-11-01","boysTotal":1056643.6756958894,"girlsTotal":11018.898987932616},{"date":"2018-12-01","boysTotal":1022322.1218654147,"girlsTotal":11954.748339502334}]},{"storeArea":"area2","totalStore":[{"date":"2018-10-01","boysTotal":91336.329372028267,"girlsTotal":0},{"date":"2018-11-01","boysTotal":78130.417193652393,"girlsTotal":0},{"date":"2018-12-01","boysTotal":100755.62136033915,"girlsTotal":0}]},{"storeArea":"area3","totalStore":[{"date":"2018-10-01","boysTotal":66604.417069221658,"girlsTotal":0},{"date":"2018-11-01","boysTotal":63882.129662868538,"girlsTotal":0},{"date":"2018-12-01","boysTotal":108781.84734382466,"girlsTotal":0}]},{"storeArea":"area4","totalStore":[{"date":"2018-10-01","boysTotal":0,"girlsTotal":1452.6775522557543},{"date":"2018-11-01","boysTotal":0,"girlsTotal":2366.423254655545},{"date":"2018-12-01","boysTotal":0,"girlsTotal":3485.5840149129986}]},{"storeArea":"area5","totalStore":[{"date":"2018-10-01","boysTotal":3237887.746893588,"girlsTotal":0},{"date":"2018-11-01","boysTotal":3218724.4935297123,"girlsTotal":0},{"date":"2018-12-01","boysTotal":2925562.2916941536,"girlsTotal":0}]},{"storeArea":"area6","totalStore":[{"date":"2018-10-01","boysTotal":291293.35185208195,"girlsTotal":0},{"date":"2018-11-01","boysTotal":171962.47103846565,"girlsTotal":0},{"date":"2018-12-01","boysTotal":95615.439855929668,"girlsTotal":0}]}]}
const resp = json.stores.map(store => ([store.storeArea, store.totalStore.reduce((a, e) => a + e.boysTotal + e.girlsTotal, 0)]))
console.log(resp);
答案 1 :(得分:0)
var results = [];
var json = {
"stores": [
{
"storeArea": "area1",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 1082220.3945979946,
"girlsTotal": 9585.8794674401543
},
{
"date": "2018-11-01",
"boysTotal": 1056643.6756958894,
"girlsTotal": 11018.898987932616
},
{
"date": "2018-12-01",
"boysTotal": 1022322.1218654147,
"girlsTotal": 11954.748339502334
}
]
},
{
"storeArea": "area2",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 91336.329372028267,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 78130.417193652393,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 100755.62136033915,
"girlsTotal": 0
}
]
},
{
"storeArea": "area3",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 66604.417069221658,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 63882.129662868538,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 108781.84734382466,
"girlsTotal": 0
}
]
},
{
"storeArea": "area4",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 0,
"girlsTotal": 1452.6775522557543
},
{
"date": "2018-11-01",
"boysTotal": 0,
"girlsTotal": 2366.423254655545
},
{
"date": "2018-12-01",
"boysTotal": 0,
"girlsTotal": 3485.5840149129986
}
]
},
{
"storeArea": "area5",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 3237887.746893588,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 3218724.4935297123,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 2925562.2916941536,
"girlsTotal": 0
}
]
},
{
"storeArea": "area6",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 291293.35185208195,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 171962.47103846565,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 95615.439855929668,
"girlsTotal": 0
}
]
}
]
}
var i = 0;
for (i=0; i<json.stores.length; i++) {
var store = json.stores[i];
var j = 0;
var boysTotal = 0;
var girlsTotal = 0;
var result = [];
for (j=0; j<store.totalStore.length; j++) {
boysTotal += parseFloat(store.totalStore[j]['boysTotal']);
girlsTotal += parseFloat(store.totalStore[j]['girlsTotal']);
}
results[i] = [];
results[i][0] = store.storeArea;
results[i][1] = boysTotal + girlsTotal;
//results[i][1] = boysTotal;
//results[i][2] = girlsTotal;
}
console.log(results);
答案 2 :(得分:0)
以下代码段将完成工作:
json.stores.forEach(function(x){
var totalboys=0, totalgirls = 0; // for clarity & flexibility
x.totalStore.forEach(function(y){
totalboys += y.boysTotal;
totalgirls += y.girlsTotal;
})
results[x.storeArea] = totalboys + totalgirls;
console.log(results[x.storeArea])
})
查看实际情况jsfiddle
答案 3 :(得分:0)
使用面向对象怎么样? json代表商店,总数组代表天数。使用store json来构建商店。反过来,其构造函数会构建一系列数组。我们教商店如何自我合计(减少每天的总数),并教商店如何自我合计(通过增加男孩和女孩的总数)。
class StoreDay {
constructor(json) {
this.date = new Date(json.date);
this.boysTotal = json.boysTotal;
this.girlsTotal = json.girlsTotal;
}
total() {
return this.boysTotal+this.girlsTotal;
}
}
class Store {
constructor(json) {
this.storeArea = json.storeArea;
this.days = json.totalStore.map(day => new StoreDay(day));
}
total() {
return this.days.reduce((acc, day) => acc + day.total(), 0);
}
}
var json = {
"stores": [
{
"storeArea": "area1",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 1082220.3945979946,
"girlsTotal": 9585.8794674401543
},
{
"date": "2018-11-01",
"boysTotal": 1056643.6756958894,
"girlsTotal": 11018.898987932616
},
{
"date": "2018-12-01",
"boysTotal": 1022322.1218654147,
"girlsTotal": 11954.748339502334
}
]
},
{
"storeArea": "area2",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 91336.329372028267,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 78130.417193652393,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 100755.62136033915,
"girlsTotal": 0
}
]
},
{
"storeArea": "area3",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 66604.417069221658,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 63882.129662868538,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 108781.84734382466,
"girlsTotal": 0
}
]
},
{
"storeArea": "area4",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 0,
"girlsTotal": 1452.6775522557543
},
{
"date": "2018-11-01",
"boysTotal": 0,
"girlsTotal": 2366.423254655545
},
{
"date": "2018-12-01",
"boysTotal": 0,
"girlsTotal": 3485.5840149129986
}
]
},
{
"storeArea": "area5",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 3237887.746893588,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 3218724.4935297123,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 2925562.2916941536,
"girlsTotal": 0
}
]
},
{
"storeArea": "area6",
"totalStore": [
{
"date": "2018-10-01",
"boysTotal": 291293.35185208195,
"girlsTotal": 0
},
{
"date": "2018-11-01",
"boysTotal": 171962.47103846565,
"girlsTotal": 0
},
{
"date": "2018-12-01",
"boysTotal": 95615.439855929668,
"girlsTotal": 0
}
]
}
]
};
let stores = json.stores.map(s => new Store(s));
let totals = stores.map(store => {
return [store.storeArea, store.total()];
});
console.log(totals);
答案 4 :(得分:-2)
您可以使用loadsh对json对象和数组执行操作。
这是减少每个唯一元素的情况。
我总是使用_.groupBy,然后在应用约简后将_.map映射到数组。在这种情况下,归约运算为_.sumBy。
var obj = [ {"name":"Jack ProjManager","count":12000}, {"name":"Jack ProjManager","count":750000}, {"name":"Joe ProjManager","count":45000} ];
var output = _(obj) .groupBy('name') .map((objs, key) => ({ 'name': key, 'count': _.sumBy(objs, 'count') })) .value(); console.log(output);