我想從我將從查詢結果中獲得的平面陣列創建一個陣列物件,并希望創建 json 結構作為回應以將其作為 api 回應傳遞。例如-平面陣列-
[{
user_id: '2311123',
user_country: 'United States',
user_city: 'ny',
ssn: 229
},
{
user_id: '451313',
user_country: 'United States',
user_city: 'abc',
ssn: 147
},
{
user_id: '65345',
user_country: 'United States',
user_city: 'abc',
ssn: 444
},
{
user_id: '763343',
user_country: 'Australia',
user_city: 'auus',
ssn: 678
}]
我想創建一個類似于預期輸出的結構
{
"United States": [
{
"ny": [
{
"user_id": "2311123",
"ssn": "7"
}
]
},
{
"abc": [
{
"user_id": "451313",
"ssn": "147"
},
{
"user_id": "65345",
"ssn": "444"
}
]
}
],
"Australia": [
{
"auus": [
{
"user_id": "763343",
"ssn": "678"
}
]
}
]
}
其中映射了 user_country 物件陣列和 user_city 物件陣列。我試過這段代碼,但沒有達到預期的輸出。:
const map = {};
results.forEach(arr => {
console.log("arr",arr)
if(map[arr.user_country]){
if(!map[arr.user_country].includes(arr.user_city))
map[arr.user_country].push(arr.user_city);
}else{
map[arr.user_country] = [arr.user_city]
}
});
console.log(map);
uj5u.com熱心網友回復:
這可能會產生預期的結果:
const array = [{ user_id: '2311123', user_country: 'United States', user_city: 'ny', ssn: 229 }, { user_id: '451313', user_country: 'United States', user_city: 'abc', ssn: 147 }, { user_id: '65345', user_country: 'United States', user_city: 'abc', ssn: 444 }, { user_id: '763343', user_country: 'Australia', user_city: 'auus', ssn: 678 }];
const map = array.reduce((map, {user_country, user_city, ...userInfo}) => {
if (!map[user_country]) {
map[user_country] = [{[user_city]: [{...userInfo}]}];
} else {
const ex = map[user_country].find(city => Object.keys(city)[0] === user_city);
if (!ex) {
map[user_country].push({[user_city]: [{...userInfo}]});
} else {
Object.values(ex)[0].push({...userInfo});
}
}
return map;
}, {});
console.log(map);
uj5u.com熱心網友回復:
const results = [{
user_id: '2311123',
user_country: 'United States',
user_city: 'ny',
ssn: 229
},
{
user_id: '451313',
user_country: 'United States',
user_city: 'abc',
ssn: 147
},
{
user_id: '65345',
user_country: 'United States',
user_city: 'abc',
ssn: 444
},
{
user_id: '763343',
user_country: 'Australia',
user_city: 'auus',
ssn: 678
}
]
const out = {};
results.forEach(i => {
out[i.user_country] = out[i.user_country] || {};
out[i.user_country][i.user_city] = out[i.user_country][i.user_city] || [];
out[i.user_country][i.user_city].push({
user_id: i.user_id,
ssn: i.ssn
})
})
console.log(out)
uj5u.com熱心網友回復:
請檢查該選項:
const results = [{
user_id: '2311123',
user_country: 'United States',
user_city: 'ny',
ssn: 229
},
{
user_id: '451313',
user_country: 'United States',
user_city: 'abc',
ssn: 147
},
{
user_id: '65345',
user_country: 'United States',
user_city: 'abc',
ssn: 444
},
{
user_id: '763343',
user_country: 'Australia',
user_city: 'auus',
ssn: 678
}];
const countries = {};
results.forEach(result => {
const {user_country, user_city, user_id, ssn} = result;
const cityList = countries[user_country] && countries[user_country][user_city] ? countries[user_country][user_city] : [];
const newCityList = [...cityList, {
user_id,
ssn
}];
countries[user_country] = {
...countries[user_country],
[user_city]: newCityList
};
});
console.log(countries);
uj5u.com熱心網友回復:
請檢查此解決方案:
const map = {};
results.forEach(arr => {
const { user_country, user_id, user_city, ssn } = arr;
if (!map[user_country]) {
map[user_country] = [];
}
if (map[user_country][user_city]) {
map[user_country][user_city].push({user_id, ssn});
} else {
map[user_country][user_city] = [{user_id, ssn}];
}
});
console.log(map)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/364764.html
標籤:javascript 数组 json 目的 映射
上一篇:獲取影像作為物件
