I would like to change the type of some elements in an (nested) array, and the only way I know is to run a for loop.
Please see the example below:
The data is of the form
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],
];
I would like to change to
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],
];
The only way I come up with is a for loop
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));
Is there better way to achieve this?