我真的很陌生,所以如果我的语言不是“ technical
”,对不起。我会学的很快。我有json
对象,我试图找到正确的方法以正确的方式在单独的数组中收集信息
我正在使用map
函数将每个键分隔为一个单独的键,因为某些键属于另一个数组,即时通讯有些混乱。我希望提供一些解释。
我在问题的底部添加了预期的解决方案,如果我不清楚的话,那是否有帮助。
以下是json对象:
var json_museums = {
"museums_costs": [
{
"museumType": "Art",
"costs": [
{
"date": "2018-10-01",
"employees": 1082220.3945979946,
"outsource": 9585.8794674401543
},
{
"date": "2018-11-01",
"employees": 1056643.6756958894,
"outsource": 11018.898987932616
},
{
"date": "2018-12-01",
"employees": 1022322.1218654147,
"outsource": 11954.748339502334
}
]
},
{
"museumType": "History",
"costs": [
{
"date": "2018-10-01",
"employees": 91336.329372028267,
"outsource": 0
},
{
"date": "2018-11-01",
"employees": 78130.417193652393,
"outsource": 0
},
{
"date": "2018-12-01",
"employees": 100755.62136033915,
"outsource": 0
}
]
},
{
"museumType": "Culture",
"costs": [
{
"date": "2018-10-01",
"employees": 66604.417069221658,
"outsource": 0
},
{
"date": "2018-11-01",
"employees": 63882.129662868538,
"outsource": 0
},
{
"date": "2018-12-01",
"employees": 108781.84734382466,
"outsource": 0
}
]
},
{
"museumType": "News",
"costs": [
{
"date": "2018-10-01",
"employees": 0,
"outsource": 1452.6775522557543
},
{
"date": "2018-11-01",
"employees": 0,
"outsource": 2366.423254655545
},
{
"date": "2018-12-01",
"employees": 0,
"outsource": 3485.5840149129986
}
]
},
{
"museumType": "Science",
"costs": [
{
"date": "2018-10-01",
"employees": 3237887.746893588,
"outsource": 0
},
{
"date": "2018-11-01",
"employees": 3218724.4935297123,
"outsource": 0
},
{
"date": "2018-12-01",
"employees": 2925562.2916941536,
"outsource": 0
}
]
},
{
"museumType": "Religious",
"costs": [
{
"date": "2018-10-01",
"employees": 291293.35185208195,
"outsource": 0
},
{
"date": "2018-11-01",
"employees": 171962.47103846565,
"outsource": 0
},
{
"date": "2018-12-01",
"employees": 95615.439855929668,
"outsource": 0
}
]
}
]
}
var results = [];
results = json_museums.museums_costs.map(function (x) {
return [x.museumType, x. visits.map(function(c) { c.date), x.costs.reduce(function (a, b, c) {
return a + b + c;
}, 0)]
});
(18) [Array(3), Array(3), Array(3), Array(3), Array(3), Array(3)]
0: (3) ["Art", "2018-10-01", outsource + employee]
1: (3) ["Art", "2018-11-01", outsource +employee]
2: (3) ["Art", "2018-12-01", outsource +employee]
3: (3) ["History", "2018-10-01", outsource + employee]
4: (3) ["History", "2018-11-01", outsource + employee]
5: (3) ["History", "2018-12-01", outsource + employee]
6: (3) ["Culture", "2018-10-01", outsource + employee]
7: (3) ["Culture", "2018-11-01", outsource + employee]
8: (3) ["Culture", "2018-12-01", outsource + employee]
9: (3) ["News", "2018-10-01", outsource + employee]
10: (3) ["News", "2018-11-01", outsource + employee]
11: (3) ["News", "2018-12-01", outsource + employee]
12: (3) ["Science", "2018-10-01", outsource + employee]
13: (3) ["Science", "2018-11-01", outsource + employee]
14: (3) ["Science", "2018-12-01", outsource + employee]
15: (3) ["Religious", "2018-10-01", outsource + employee]
16: (3) ["Religious", "2018-11-01", outsource + employee]
17: (3) ["Religious", "2018-12-01", outsource + employee]
最佳答案
您可以reduce
以空数组开头的顶级数组作为累加器,然后对成本进行map
并将结果数组的元素推入累加器:
var json_museums = {"museums_costs":[{"museumType":"Art","costs":[{"date":"2018-10-01","employees":1082220.3945979946,"outsource":9585.8794674401543},{"date":"2018-11-01","employees":1056643.6756958894,"outsource":11018.898987932616},{"date":"2018-12-01","employees":1022322.1218654147,"outsource":11954.748339502334}]},{"museumType":"History","costs":[{"date":"2018-10-01","employees":91336.329372028267,"outsource":0},{"date":"2018-11-01","employees":78130.417193652393,"outsource":0},{"date":"2018-12-01","employees":100755.62136033915,"outsource":0}]},{"museumType":"Culture","costs":[{"date":"2018-10-01","employees":66604.417069221658,"outsource":0},{"date":"2018-11-01","employees":63882.129662868538,"outsource":0},{"date":"2018-12-01","employees":108781.84734382466,"outsource":0}]},{"museumType":"News","costs":[{"date":"2018-10-01","employees":0,"outsource":1452.6775522557543},{"date":"2018-11-01","employees":0,"outsource":2366.423254655545},{"date":"2018-12-01","employees":0,"outsource":3485.5840149129986}]},{"museumType":"Science","costs":[{"date":"2018-10-01","employees":3237887.746893588,"outsource":0},{"date":"2018-11-01","employees":3218724.4935297123,"outsource":0},{"date":"2018-12-01","employees":2925562.2916941536,"outsource":0}]},{"museumType":"Religious","costs":[{"date":"2018-10-01","employees":291293.35185208195,"outsource":0},{"date":"2018-11-01","employees":171962.47103846565,"outsource":0},{"date":"2018-12-01","employees":95615.439855929668,"outsource":0}]}]}
var results = json_museums.museums_costs.reduce((a, x) => {
a.push(...x.costs.map(cost => [x.museumType, cost.date, cost.outsource + cost.employees]));
return a;
}, []);
console.log(results);
做到这一点的另一种方法是使用
flatMap
,尽管这种方法并未得到普遍支持。关于javascript - 如何以特定顺序正确收集json数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54244636/