Home > Software engineering >  How to convert values from certain keys of an array to numbers?
How to convert values from certain keys of an array to numbers?

Time:05-20

I have the following array of objects

const mi_array = [{
  agrupacion: "Total país",
  col2015: '81.8',
  col2016: '86.4',
  col2017: '67.3',
  col2018: '70.8',
  col2019: '67.6'
},{
  agrupacion: "otra cosa",
  col2015: '90.8',
  col2016: '67.4',
  col2017: '39.3',
  col2018: '50.8',
  col2019: '95.6'
}];

I need to transform this into something like this:

const new_array = [{
  name: "Total país",
  data: [81.8, 86.4, 67.3, 70.8, 67.6]
}, {
  name: "otra cosa",
  data: [90.8, 67.4, 39.3, 50.8, 95.6]
}];

I tried this but since values on keys col2015 thru 2019 are all strings, it doesn't work as intended

const result = mi_array.map(e => ({
  name: e.agrupacion,
  data: Object.values(e).filter(e => typeof e == 'number')
}))

console.log(result)

output:

[{
  data: [],
  name: "Total país"
}, {
  data: [],
  name: "otra cosa"
}]

I know it will work if I can transform those values into numbers somehow so i tried to achieve that with this:

for(let key of Object.keys(mi_array))  mi_array[key] =  mi_array[key]

console.log(mi_array);

however my output is:

[NaN, NaN]

another attempt:

var result = Object.keys(mi_array).map(function(key) {
  return [Number(key), mi_array[key]];
}); 

console.log(result); 

another failed output:

[[0, {
  agrupacion: "Total país",
  col2015: "81.8",
  col2016: "86.4",
  col2017: "67.3",
  col2018: "70.8",
  col2019: "67.6"
}], [1, {
  agrupacion: "otra cosa",
  col2015: "90.8",
  col2016: "67.4",
  col2017: "39.3",
  col2018: "50.8",
  col2019: "95.6"
}]]

Is there an efficient way of doing this?

CodePudding user response:

You could destructure name and take from the rest only the values as data, after mapping with Number.

This approach takes the original order of the object.

const
    mi_array = [{ agrupacion: "Total país", col2015: '81.8', col2016: '86.4', col2017: '67.3', col2018: '70.8', col2019: '67.6' }, { agrupacion: "otra cosa", col2015: '90.8', col2016: '67.4', col2017: '39.3', col2018: '50.8', col2019: '95.6' }],
    result = mi_array.map(({ agrupacion: name, ...o }) => ({
        name,
        data: Object.values(o).map(Number)
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

const new_array = [];

for(let i = 0; i < mi_array.length;   i) {

  const new_obj = {"name":"", "data":[]};

  for(const key in mi_array[i]) {

    const num = Number(mi_array[i][key]);

    if(isNaN(num)) {
      new_obj.name = mi_array[i][key];
    } else {
      new_obj.data.push(num);
    }

  }

  new_array.push(new_obj);
}

CodePudding user response:

You could use it.

First, map array and take properties agrupacion and rest props for map in data and convert to Number.

const newArr = mi_array.map(({agrupacion, ...props})=> ({
    name:agrupacion,
  data: [...Object.values(props).map(value => Number(value))]
}))

CodePudding user response:

const mi_array = [{
  agrupacion: "Total país",
  col2015: '81.8',
  col2016: '86.4',
  col2017: '67.3',
  col2018: '70.8',
  col2019: '67.6'
},{
  agrupacion: "otra cosa",
  col2015: '90.8',
  col2016: '67.4',
  col2017: '39.3',
  col2018: '50.8',
  col2019: '95.6'
}];

new_array = []
for (var i = 0; i < mi_array.length; i  )
{
    new_dict = {"Data": []}
    for (item in mi_array[i])
    {
      x = parseFloat(mi_array[i][item])
      if (!(!(!(x))))
      {
          new_dict["Name"] = mi_array[i][item]
      }
      else
      {
          new_dict["Data"].push(x);
      }
    }
    new_array.push(new_dict);
}
console.log(new_array)

  • Related