如何在JavaScript中更改数组中某些元素的数据类型

时间:2018-05-11 00:45:19

标签: javascript arrays

我想改变(嵌套)数组中某些元素的类型,我知道的唯一方法是运行for循环。

请参阅以下示例:

数据的格式为

var chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];

我想改为

var new_data = [
  [new Date("1980/01/23"), 95, 100, 98, 110],
  [new Date("1980/01/24"), 98, 98, 102, 103],
  [new Date("1980/01/25"), 90, 102, 95, 105],
  [new Date("1980/01/26"), 93, 95, 103, 103],
  [new Date("1980/01/27"), 94, 103, 104, 105],
];

我想出的唯一方法是for循环

function transform(arr) {
  var new_arr = [];

  for (var i = 0; i < arr.length; i++) {
    var sub_arr = [];
    for (var j = 0; j < arr[i].length; j++) {
      if (j == 0) {
        sub_arr.push(new Date(arr[i][j]));
      } else {
        sub_arr.push(arr[i][j]);
      }
    }
    new_arr.push(sub_arr);
  }
  return new_arr
}
alert(transform(chartdata));

有没有更好的方法来实现这一目标?

4 个答案:

答案 0 :(得分:6)

使用.map将一个数组转换为另一个数组:

&#13;
&#13;
const chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];
const newData = chartdata.map(([dateStr, ...rest]) => [new Date(dateStr), ...rest]);
console.log(newData);
&#13;
&#13;
&#13;

对于ES5兼容性,您无法使用休息/传播和箭头功能,因此另一个选项是:

&#13;
&#13;
var chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];
var newData = chartdata.map(function(arr) {
  var date = new Date(arr[0]);
  return [date].concat(arr.slice(1));
});
console.log(newData);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

当然你可以这样做:

const chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];

const newData = chartdata.map(arr => {
  arr[0] = new Date(arr[0]);
  return arr;
});

console.log(newData);

如果您不想改变原始数组:

const newData = chartdata.map(([...arr]) => {
    arr[0] = new Date(arr[0]);
    return arr;
});

你也可以在一行中使用param destruct这样做:

const newData = chartdata.map(([date, ...rArr]) => [new Date(date), ...rArr]);

答案 2 :(得分:2)

实际上是的,您使用map复制了每个内部数组的内容很多次,并且分配新值也可以。

这里有一个有效的例子:

&#13;
&#13;
var chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];

function transform(arr) {
  var new_arr = [];

  for (var i = 0; i < arr.length; i++) {
    var sub_arr = [];
    for (var j = 0; j < arr[i].length; j++) {
      if (j == 0) {
        sub_arr.push(new Date(arr[i][j]));
      } else {
        sub_arr.push(arr[i][j]);
      }
    }
    new_arr.push(sub_arr);
  }
  return new_arr
}

function improvedTransform(arr) {
  return arr.map(item => {
    item[0] = new Date(item[0]);
    return item;
  })
}
console.log(transform(chartdata));
console.log("-------");
console.log(improvedTransform(chartdata));
&#13;
&#13;
&#13;

答案 3 :(得分:1)

您可以使用map()从现有阵列创建新阵列:

&#13;
&#13;
var chartdata = [
  ["1980/01/23", 95, 100, 98, 110],
  ["1980/01/24", 98, 98, 102, 103],
  ["1980/01/25", 90, 102, 95, 105],
  ["1980/01/26", 93, 95, 103, 103],
  ["1980/01/27", 94, 103, 104, 105],
];

var new_data = chartdata.map(function(i){
  var date = new Date(i[0]);
  date = [date].concat(i.slice(1));
  return date;
})

console.log(new_data);
&#13;
&#13;
&#13;