Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I transform the data from this API into an array of objects organized by a specific prop?

I’m getting this data from an API:

 let dataFromApi = [
    {
      // lots of other props
      name: 'super cool place',
      address: 'address of food place',
      classification: 'food',
    },
    {
      // lots of other props
      name: 'name of tech place',
      address: 'address of tech place',
      classification: 'tech',
    },
    {
      // lots of other props
      name: 'name of clothes place',
      address: 'address of clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another clothes place',
      address: 'address of another clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another tech place',
      address: 'address of another tech place',
      classification: 'tech',
    },
  ]

What I’d like to do is create a new array of objects that has 2 things:

  1. A prop called classification;

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  2. A prop called establishments, which is an array of objects containing all the establishments matching the classification prop.

Meaning something like this:

let establishmentsArray = [
    {
      classification: 'food',
      establishments: [
        {
          name: 'name of food place',
          address: 'address of food place',
          classification: 'food',
          // rest of its props
        },
        // Lots of other food places places
      ],
    },
    {
      classification: 'clothes',
      establishments: [
        {
          name: 'name of clothes place',
          address: 'address of clothes place',
          classification: 'clothes',
          // rest of its props
        },
        {
          name: 'name of another clothes place',
          address: 'address of another clothes place',
          classification: 'clothes',
          // rest of its props
        },
        // Lots of other clothes places
      ],
    },
    {
      classification: 'tech',
      establishments: [
        {
          name: 'name of tech place',
          address: 'address of tech place',
          classification: 'tech',
          // rest of its props
        },
        {
          name: 'name of another tech place',
          address: 'address of another tech place',
          classification: 'tech',
          // rest of its props
        },
        // Lots of other tech places
      ],
    },
  ]

Thanks for the help in advance!

>Solution :

Looks like you’re trying to group by single field which is a good use-case for array.reduce:

let dataFromApi = [
    {
      // lots of other props
      name: 'super cool place',
      address: 'address of food place',
      classification: 'food',
    },
    {
      // lots of other props
      name: 'name of tech place',
      address: 'address of tech place',
      classification: 'tech',
    },
    {
      // lots of other props
      name: 'name of clothes place',
      address: 'address of clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another clothes place',
      address: 'address of another clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another tech place',
      address: 'address of another tech place',
      classification: 'tech',
    },
  ]
  
  let output = dataFromApi.reduce((acc, cur) => {
      let { classification, ...rest} = cur;
      let prev = acc.find(x => x.classification === classification);
      if(!prev) {
          acc.push({classification, establishments: [cur]});
      } else {
          prev.establishments.push(cur);
      }
      return acc;
  }, []);
  
  console.log(output);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading