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 e 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?
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 e 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?
Share Improve this question edited May 11, 2018 at 7:24 CertainPerformance 372k55 gold badges352 silver badges357 bronze badges asked May 11, 2018 at 0:45 skippyhoskippyho 1493 silver badges10 bronze badges 3- Define "better". – user2437417 Commented May 11, 2018 at 0:45
- @ Crazy Train, sorry for the imprecise words. I shall use 'efficient' or 'faster' way. – skippyho Commented May 11, 2018 at 1:15
- The answers you received below are not necessarily written with performance as the primary objective. As to what is fastest, only performance tests will tell you that, and it will vary between implementations. – user2437417 Commented May 11, 2018 at 1:18
4 Answers
Reset to default 6Use .map
to transform one array into another:
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);
For ES5 patibility, you won't be able to use rest/spread and arrow functions, so another option is:
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);
Sure you could simply do this:
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);
If you don't want to mutate the original array:
const newData = chartdata.map(([...arr]) => {
arr[0] = new Date(arr[0]);
return arr;
});
You can also do it in one line with param destructing like this:
const newData = chartdata.map(([date, ...rArr]) => [new Date(date), ...rArr]);
actually yes, you are copying a lot of times the content of each inner array, using map
and assigning the new value would work.
here you have a working example:
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));
You can use map()
to create a new array from the existing one:
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);