1

I'm newbie in programming, so i need some help. I have array of objects. I need to sort it and turn it into an array of objects, which will contain the key city, then an array of objects from the leaders, and an array of coffee shops.

  let arr = [
    {city: 'New York', boss: 'John', name: 'Caffe 1'},
    {city: 'New York', boss: 'John', name: 'Caffe 2'},
    {city: 'New York', boss: 'Ben', name: 'Caffe 3'},
    {city: 'New York', boss: 'Ben', name: 'Caffe 4'},
    {city: 'Washington', boss: 'Lisa', name: 'Caffe 5'},
    {city: 'Washington', boss: 'Lisa', name: 'Caffe 6'},
    {city: 'Washington', boss: 'Kate', name: 'Caffe 7'},
    {city: 'Washington', boss: 'Kate', name: 'Caffe 8'},
    {city: 'Los Angeles', boss: 'Joe', name: 'Caffe 9'}

  ]

I need to get something like this.

    let result =  [
      {city: 'New York', 
      boss:[
        {
          name: 'John', 
          caffes:[
            {name: 'Caffe 1'},
            {name: 'Caffe 2'}
                  ]
                },
        {name: 'Ben', 
          caffes: [
            {name: 'Caffe 3'},
            {name: 'Caffe 4'}
                  ]
                }     
            ]
      },
      {city: 'Washington', 
      boss:[
        {
          name: 'Lisa', 
          caffes:[
            {name: 'Caffe 5'},
            {name: 'Caffe 6'}
                  ]
                },
        {name: 'Kate', 
          caffes: [
            {name: 'Caffe 7'},
            {name: 'Caffe 8'}
                  ]
                }     
            ]
      },
      {city: 'Los Angeles', 
      boss:[
        {
          name: 'Joe', 
          caffes:[
            {name: 'Caffe 9'},
                  ]
                },
            ]
      },
      ]

The best what i can do is this:

    function sortData(data) {
    let groups = []; 

    for (let element of data) {
        let existingGroups = groups.filter((group) => group.city == element.city); 
        if (existingGroups.length > 0) {
            existingGroups[0].city = element.city;
            existingGroups[0].boss =  element.boss;

        } else {
            let newGroup = {
                city: element.city,
        boss: element.boss

            };
            groups.push(newGroup); 
        }
    }
    return groups; 
}

And its makes the:

[ { city: 'New York', boss: 'Ben' },
  { city: 'Washington', boss: 'Kate' },
  { city: 'Los Angeles', boss: 'Joe' } ]

But i dont know how to make it more complex.

I trying many ways, but any of them can't help me. How can i do this?

4
  • 1
    And remember to search first because you're definitely not the first person to want to do this =) Commented Jul 5, 2022 at 22:12
  • 1
    As Mike mentions, search. Here's one example. stackoverflow.com/questions/40774697/… Commented Jul 5, 2022 at 22:24
  • @Mike'Pomax'Kamermans, naturaly, i trying to research. But, i'm don't found what i need :( Commented Jul 5, 2022 at 22:26
  • @Nikki9696, i saw this post, but its still not noot what i need. I needed to sorted it by 2 keys, and this is hard for me. sort for one key i do by myself. Commented Jul 5, 2022 at 22:32

1 Answer 1

3

It's true that this is at its heart a very straight forward 'group by' as in this duplicate How can I group an array of objects by key?. But your data requires another level of grouping by boss within the first grouping by city.

Here's an example grouping into an object, iterating each element in a for...of loop then retrieving or creating the relevant properties using logical nullish assignment (??=). This is done first for the city and then again for the city.boss. Finally the we take the Object.values() of the grouped object as the result, and map over it in order to also convert each boss object to a an array of values.

let arr = [{ city: 'New York', boss: 'John', name: 'Caffe 1' }, { city: 'New York', boss: 'John', name: 'Caffe 2' }, { city: 'New York', boss: 'Ben', name: 'Caffe 3' }, { city: 'New York', boss: 'Ben', name: 'Caffe 4' }, { city: 'Washington', boss: 'Lisa', name: 'Caffe 5' }, { city: 'Washington', boss: 'Lisa', name: 'Caffe 6' }, { city: 'Washington', boss: 'Kate', name: 'Caffe 7' }, { city: 'Washington', boss: 'Kate', name: 'Caffe 8' }, { city: 'Los Angeles', boss: 'Joe', name: 'Caffe 9' }]

const groupedByCity = {};

for (const { city, boss, name } of arr) {
  const cityGroup = (groupedByCity[city] ??= { city, boss: {} });
  const bossGroup = (cityGroup.boss[boss] ??= { name: boss, caffes: [] });

  bossGroup.caffes.push({ name })
}

const result = Object.values(groupedByCity)
  .map(o => ({ ...o, boss: Object.values(o.boss) }));

console.log(result)

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

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.