【问题标题】:Map Key value to create a json structure with nested objects using javascript映射键值以使用 javascript 创建具有嵌套对象的 json 结构
【发布时间】:2022-01-02 13:40:39
【问题描述】:

我想从我将从查询结果中获得的平面数组创建一个数组对象,并希望创建 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);

【问题讨论】:

  • 有什么理由需要城市作为数组中的对象而不是对象的属性?以United States: { ny: [], abc: [] } 为例。

标签: javascript arrays json object mapping


【解决方案1】:

这可能会产生预期的结果:

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

【讨论】:

    【解决方案2】:

    请检查此解决方案:

    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)
    

    【讨论】:

      【解决方案3】:

      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)

      【讨论】:

      • 不是 OP 正在寻找的输出,这就是我添加该评论的原因。但这作为输出 fwiw 更有意义。 PS不要忘记将ssn强制为字符串。
      【解决方案4】:

      请检查该选项:

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

      【讨论】:

        猜你喜欢
        • 2014-06-08
        • 1970-01-01
        • 2023-01-11
        • 2019-09-16
        • 1970-01-01
        • 2021-07-06
        • 2017-02-14
        • 2020-06-16
        • 1970-01-01
        相关资源
        最近更新 更多