I’m pretty new to Javascript, and want to swap the items of an array in an array. Example:
{
"data": [
[null, 1353064450],
[null, 1353064460],
[null, 1353064470],
[null, 1353064480],
[null, 1353064490]
],
"label": "cpu"
}
should become:
{
"data": [
[1353064450, null],
[1353064460, null],
[1353064470, null],
[1353064480, null],
[1353064490, null]
],
"label": "cpu"
}
I probably have to iterate through the array and create a new array? Any pointers would be great!
With modern JS, you can just do
obj.data = obj.data.map(([x, y]) => [y, x]).Support across browsers might be patchy, so you should use a transpiler that turns this into ES5, at least for the near future.
Original answer:
Use the
Array.reversemethod: