如何将值从数组的某些键转换为数字?

How to convert values from certain keys of an array to numbers?

我有以下对象数组

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'
}];

我需要把它改造成这样的东西:

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]
}];

我试过了,但由于键 col2015 到 2019 的值都是字符串,所以它无法按预期工作

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

console.log(result)

输出:

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

我知道如果我能以某种方式将这些值转换成数字,它就会起作用,所以我试图用这个来实现:

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

console.log(mi_array);

然而我的输出是:

[NaN, NaN]

另一次尝试:

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

console.log(result); 

另一个失败的输出:

[[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"
}]]

有没有有效的方法来做到这一点?

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

您可以解构 name 并在使用 Number.

映射后仅从其余值中获取 data 的值

此方法采用对象的原始顺序。

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

你可以用它。

首先,映射数组并在 data 中获取属性 agrupacion 和映射的其余道具,然后转换为 Number.

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

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)