3

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. 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

{
  "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. I have tried this code, but coudnt achieve the expected 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);
1
  • 1
    Any reason you need cities to be objects in an array rather than properties of an object? United States: { ny: [], abc: [] }, for example. Commented Nov 24, 2021 at 6:38

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

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)

1 Comment

Not the output the OP was looking for which is why I added that comment. But this makes more sense as output fwiw. PS don't forget to coerce the ssn to a string.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.