Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of data in javascript

I have the following type of data assigned to var dataTransformation, which I'm taking from the user in apache superset using metric.

{country: "Afghanistan", region: "South Asia", year: 1960, value: 91.779}
{country: "Afghanistan", region: "South Asia", year: 1961, value: 91.492}
{country: "Afghanistan", region: "South Asia", year: 1962, value: 91.195}
{country: "Bangladesh", region: "South Asia", year: 1960, value: 94.865}
{country: "Bangladesh", region: "South Asia", year: 1961, value: 94.722}
{country: "Bangladesh", region: "South Asia", year: 1962, value: 94.502}
{country: "Canada", region: "North America", year: 1960, value: 30.939}
{country: "Canada", region: "North America", year: 1961, value: 30.332}
{country: "Canada", region: "North America", year: 1962, value: 29.506}

But I want to convert it into the below format. I tried it using the array map function, loops, and string concatenation but it is not efficient. Is there any way to do it in javascript?

{
   "South Asia": [
      ["Afghanistan", 91.779, 91.492, 91.195],
      ["Bangladesh", 94.865, 94.722, 94.502],
   ],
   "North America":[
      ["Canada", 30.939, 30.332, 29.506],
    ],
}

I'm expecting a guide on how to do it, not fully working code.

like image 489
mr.volatile Avatar asked Dec 13 '22 07:12

mr.volatile


2 Answers

const data = [{
  country: "Afghanistan",
  region: "South Asia",
  year: 1960,
  value: 91.779
},{
  country: "Afghanistan",
  region: "South Asia",
  year: 1961,
  value: 91.492
},{
  country: "Afghanistan",
  region: "South Asia",
  year: 1962,
  value: 91.195
},{
  country: "Bangladesh",
  region: "South Asia",
  year: 1960,
  value: 94.865
},{
  country: "Bangladesh",
  region: "South Asia",
  year: 1961,
  value: 94.722
},{
  country: "Bangladesh",
  region: "South Asia",
  year: 1962,
  value: 94.502
},{
  country: "Canada",
  region: "North America",
  year: 1960,
  value: 30.939
},{
  country: "Canada",
  region: "North America",
  year: 1961,
  value: 30.332
},{
  country: "Canada",
  region: "North America",
  year: 1962,
  value: 29.506
}];

const res = data.reduce((acc, {country, region, year, value}) => {
  acc = {
    ...acc,
    [region]: {
      ...acc[region],
      [country]: !acc?.[region]?.[country] ? [value] : acc[region][country].concat(value) 
    }
  }
  return acc;
}, { });

console.log(res);
like image 79
kyun Avatar answered Dec 22 '22 15:12

kyun


    const data = [
      { country: "Afghanistan", region: "South Asia", year: 1960, value: 91.779 },
      { country: "Afghanistan", region: "South Asia", year: 1961, value: 91.492 },
      { country: "Afghanistan", region: "South Asia", year: 1962, value: 91.195 },
      { country: "Bangladesh", region: "South Asia", year: 1960, value: 94.865 },
      { country: "Bangladesh", region: "South Asia", year: 1961, value: 94.722 },
      { country: "Bangladesh", region: "South Asia", year: 1962, value: 94.502 },
      { country: "Canada", region: "North America", year: 1960, value: 30.939 },
      { country: "Canada", region: "North America", year: 1961, value: 30.332 },
      { country: "Canada", region: "North America", year: 1962, value: 29.506 },
    ];


    const transformation = data.reduce((acc, curr) => {
      const { country, region, year, value } = curr;
      if (!acc[region]) {
        acc[region] = [];
        acc[region].push([country, value]);
        return acc;
      }

      let isCountryExist = false;

      acc[region].forEach((el) => {
        if (el.includes(country)) {
          isCountryExist = true;
          el.push(value);
        }
      });
      if (!isCountryExist) {
        acc[region].push([country, value]);
      }
      return acc;
    }, {});

    console.log(transformation);
like image 25
decpk Avatar answered Dec 22 '22 16:12

decpk