지도 핵심 가치를 만들 json 구조를 가진 중첩된 개체 javascript

0

질문

를 만들고 싶은데 객체의 편에서는 평면 배열하는 것이 점점에서 쿼리 결과를 만들려는 json 구조물에 대한 응답으로 전달하 api 를 응답합니다. For eg- 평면 배열

[{
    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);
arrays javascript json mapping
2021-11-24 06:12:50
4

최고의 응답

2

이것을 생산할 수 있는 예상한 결과:

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

2021-11-24 06:42:25
1

을 확인하시기 바랍 이 솔루션:

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)
2021-11-24 08:23:29
0

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)

2021-11-24 06:39:44

지 않 출력 OP 을 찾고 있었다는 이유는 내가 하는다. 하지만 이것은 더 감각으로 출력 fwiw. PS 하는 것을 잊지 마시키도록 ssn 하는 문자열을 사용합니다.
Andy
0

는지 확인하십시오 옵션:

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

2021-11-24 06:43:28

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................