获取嵌套 JSON 中每个数组的数量
2020-01-14
303
我有一个像这样的嵌套 json 数据
{
"data": {
"COUNTRIES": [
{
"COUNTRY": "INDIA",
"CITIES": [
{
"CITY": "BANGALORE",
"STORE": [
{
"STORE_NAME": "amazon",
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "1"
},
{
"APPLIANCE_ID": "2"
},
{
"APPLIANCE_ID": "3"
},
{
"APPLIANCE_ID": "4"
},
{
"APPLIANCE_ID": "5"
},
{
"APPLIANCE_ID": "6"
},
{
"APPLIANCE_ID": "7"
}
]
}
]
}
]
},
{
"CITY": "DELHI",
"STORE": [
{
"STORE_NAME": "amazon",
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "1"
},
{
"APPLIANCE_ID": "2"
},
{
"APPLIANCE_ID": "3"
},
{
"APPLIANCE_ID": "4"
},
{
"APPLIANCE_ID": "5"
},
{
"APPLIANCE_ID": "6"
},
{
"APPLIANCE_ID": "7"
}
]
}
]
}
]
}
]
},
{
"COUNTRY": "UNITED KINGDOM",
"CITIES": [
{
"CITY": "BIRMINGHAM",
"STORE": []
}
]
}
]
}
]
}
}
我必须修改并计算每个级别的每个数组,结果如下
{
"data": {
"COUNTRIES": [
{
"COUNTRY": "INDIA",
"CITIES": [
{
"CITY": "BANGALORE",
"STORE": [
{
"STORE_NAME": "AMAZON",
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "1"
},
{
"APPLIANCE_ID": "2"
},
{
"APPLIANCE_ID": "3"
},
{
"APPLIANCE_ID": "4"
},
{
"APPLIANCE_ID": "5"
},
{
"APPLIANCE_ID": "6"
},
{
"APPLIANCE_ID": "7"
}
]
}
],
"DEVICE_CNT": 7,
"APPLIANCES_CNT": 35
}
],
"STORE_CNT": 1,
"DEVICE_CNT": 7,
"APPLIANCES_CNT": 35
},
{
"CITY": "DELHI",
"STORE": [],
"STORE_CNT": 0
}
],
"CITY_CNT": 2,
"STORE_CNT": 1,
"DEVICE_CNT": 7,
"APPLIANCES_CNT": 35
},
{
"COUNTRY": "UNITED KINGDOM",
"CITIES": [
{
"CITY": "BIRMINGHAM",
"STORE": [
{
"STORE_NAME": "M&S",
"LATTITUDE": 52.486243,
"LONGITUDE": -1.890401,
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "1"
},
{
"APPLIANCE_ID": "2"
},
{
"APPLIANCE_ID": "3"
},
{
"APPLIANCE_ID": "4"
}
],
"APPLIANCES_CNT": 4
}
],
"DEVICE_CNT": 1,
"APPLIANCES_CNT": 4
}
],
"STORE_CNT": 1,
"DEVICE_CNT": 1,
"APPLIANCES_CNT": 4
}
],
"CITY_CNT": 1,
"STORE_CNT": 1,
"DEVICE_CNT": 1,
"APPLIANCES_CNT": 4
}
],
"COUNTRY_CNT": 2,
"CITY_CNT": 3,
"STORE_CNT": 2,
"DEVICE_CNT": 8,
"APPLIANCES_CNT": 39
}
}
我无法获得上层的总数,例如在 CITIES,电器的总数必须是 BANGLORE 所有电器的总数。我只能获得特定级别的计数,而不能获得其上方的计数。
这是我到目前为止所做的
let levels = {
0: ["COUNTRY_CNT", "CITY_COUNT", "STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
1: ["CITY_COUNT", "STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
2: ["STORE_COUNT", "DEVICE_COUNT", "APPLIANCES_COUNT"],
3: ["DEVICE_COUNT", "APPLIANCES_COUNT"],
4: ["APPLIANCES_COUNT"]
};
let counts = {COUNTRIES:0,CITIES:0,STORE:0,DEVICE_DETAILS:0,APPLIANCES:0};
let count_mapping = {
CITY_COUNT:"CITIES",
STORE_COUNT:"STORE",
DEVICE_COUNT: "DEVICE_DETAILS",
APPLIANCES_COUNT: "APPLIANCES",
COUNTRY_CNT:"COUNTRIES"
};
let total_count= {COUNTRIES:[],CITIES:[],STORE:[],DEVICE_DETAILS:[],APPLIANCES:[]};
async function parse(dictionary,level) {
console.log(dictionary);
for (item in dictionary) {
counts = {COUNTRIES:0,CITIES:0,STORE:0,DEVICE_DETAILS:0,APPLIANCES:0};
for (subItem in dictionary[item]) {
let b = item;
if (dictionary[b]) {
let sub = subItem;
let main = dictionary[b][subItem];
if (Array.isArray(dictionary[b][subItem])) {
await parse(dictionary[b][subItem],level+1);
if ( dictionary[b] && Array.isArray(main)){
counts[sub]=dictionary[b][sub].length;
}
await add(b,dictionary,level);
}
}
}
}
}
function add(b,dictionary,level) {
for(params in levels[level]){
if(dictionary[b])
dictionary[b][levels[level][params]] = counts[count_mapping[levels[level][params]]]
}
}
如何获得我想要的输出?
1个回答
您可以像之前一样运行 async-await 来获取初始数据。
然后,您应该在数组上结合使用
.length
、
.map()
和
.reduce()
方法。
我创建了以下代码来向您展示如何对城市计数和商店计数进行计数。您也可以对其他属性执行类似操作。
let dictionary = {
"datetime": "2020-01-09T11:00:01+05:30",
"timestamp": 1578547801392,
"status": "success",
"code": 200,
"message": "OK",
"data": {
"COUNTRIES": [
{
"COUNTRY": "INDIA",
"CITIES": [
{
"CITY": "BANGALORE",
"STORE": [
{
"STORE_NAME": "amazon",
"LATTITUDE": 12.9316666,
"LONGITUDE": 77.6226849,
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "0235ffc80001"
},
{
"APPLIANCE_ID": "0235ffc80002"
},
{
"APPLIANCE_ID": "0602ffd00701"
},
{
"APPLIANCE_ID": "0615ff600901"
},
{
"APPLIANCE_ID": "0605ffd00702"
},
{
"APPLIANCE_ID": "0235ffb80b01"
},
{
"APPLIANCE_ID": "0235ffb80b02"
}
]
}
]
}
]
},
{
"CITY": "DELHI",
"STORE": [
{
"STORE_NAME": "amazon",
"LATTITUDE": 12.9316666,
"LONGITUDE": 77.6226849,
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "0235ffc80001"
},
{
"APPLIANCE_ID": "0235ffc80002"
},
{
"APPLIANCE_ID": "0602ffd00701"
},
{
"APPLIANCE_ID": "0615ff600901"
},
{
"APPLIANCE_ID": "0605ffd00702"
},
{
"APPLIANCE_ID": "0235ffb80b01"
},
{
"APPLIANCE_ID": "0235ffb80b02"
}
]
}
]
}
]
}
]
},
{
"COUNTRY": "UNITED KINGDOM",
"CITIES": [
{
"CITY": "BIRMINGHAM",
"STORE": [
{
"STORE_NAME": "M&S",
"LATTITUDE": 52.486243,
"LONGITUDE": -1.890401,
"DEVICE_DETAILS": [
{
"POI": "POI1",
"APPLIANCES": [
{
"APPLIANCE_ID": "100202640001"
},
{
"APPLIANCE_ID": "0106ffe0150"
},
{
"APPLIANCE_ID": "0106ffe01501"
},
{
"APPLIANCE_ID": "0106ffe01503"
}
]
}
]
}
]
}
]
}
]
}
};
let newJson = dictionary;
newJson.data.COUNTRIES.COUNTRY_CNT = newJson.data.COUNTRIES.length
newJson.data.COUNTRIES = newJson.data.COUNTRIES.map( country =>{
country.CITY_CNT = country.CITIES.length;
country.CITIES = country.CITIES.map(city => {
city.STORE_CNT = city.STORE.length
return city;
});
country.STORE_CNT = country.CITIES.reduce((total, city) => {
return total + city.STORE_CNT
}, 0);
return country;
});
console.log(newJson);
Prateek
2020-01-14