gpt4 book ai didi

javascript - 获取嵌套 JSON 中每个数组的计数

转载 作者:行者123 更新时间:2023-11-28 16:52:45 25 4
gpt4 key购买 nike

我有一个像这样的嵌套 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]]]
}
}

如何获得所需的输出?

最佳答案

您可以运行 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);

关于javascript - 获取嵌套 JSON 中每个数组的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59729216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com