简体   繁体   English

Map 使用 javascript 创建具有嵌套对象的 json 结构的键值

[英]Map Key value to create a json structure with nested objects using javascript

I want to create an Object of array from the flat array which i will be getting from the query results and want to create json structure as a response to pass it as api response.我想从平面数组中创建一个 Object 数组,我将从查询结果中获得该数组,并希望创建 json 结构作为响应以将其作为 api 响应传递。 For eg- Flat 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
  }]

I want to create a structure like- Expected Output我想创建一个结构,如预期的 Output

{
  "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"
        }
      ]
    }
  ]
}

which has user_country array of objects and user_city array of objects mapped.其中映射了 user_country 对象数组和 user_city 对象数组。 I have tried this code, but coudnt achieve the expected output.:我试过这段代码,但无法达到预期的 output.:

  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);

This could produce expected results:这可能会产生预期的结果:

 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);

 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)

Please check that option:请检查该选项:

 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);

Please check this solution:请检查此解决方案:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AngularJS中创建地图(键,值)结构,并填充JSON中的数据 - Create map (Key,Value) structure in AngularJS and fill with data from JSON 使用.map 解析JavaScript中的动态JSON结构,创建具体的ZA3CBC3F9D0CE1F2C1559CD71D6 - Parsing dynamic JSON structure in JavaScript using .map to create specific arrays? 使用 map function 更改嵌套结构中的对象 - Change objects in nested structure using map function Append 通过在嵌套的 json 结构中复制 javascript 中特定键的值的键值对 - Append a key value pair by copying the value of a specific key in javascript in nested json structure 将对象数组转换为 Javascript 中的键值结构 - Convert Array of Objects to Key Value Structure in Javascript 按 JavaScript 中的键和嵌套值过滤对象数组 - Filter an array of objects by key and nested value in JavaScript 将键名更改为嵌套 JSON 的键值结构 - Change the key name to be the key value structure of a nested JSON 如何通过使用同一数据中的另一个值从嵌套的 JSON 结构中获取键的值 - How to get the value of a key from a nested JSON structure by using another value in the same data javascript-动态创建嵌套的json对象 - javascript - Dynamically create nested json objects 如何将嵌套的JSON数据结构减少为映射JavaScript - How to reduce nested JSON data structure into a map JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM