user3767164 user3767164 - 1 year ago 54
jQuery Question

Split a 2D array into single arrays

I am bit new in JQuery ,,I have a 2D Array

2DArray =[2.0,6.31]
[3.0,6.09]
[4.0,7.44]


I want to split it into 2 One dimensional Array like this:

[2.0, 3.0, 4.0]
[6.31, 6.09, 7.44]


I have read posts about convert single array into multidimensional array but not vice versa ...

Any suggestion would be helpful

Answer Source

This should about do it, you have to loop:

var twoDArray =[[2.0,6.31],
         [3.0,6.09],
         [4.0,7.44]];

var xArray = [];
var yArray = [];

$.each(twoDArray, function(index, value) {
    xArray.push(value[0]);
    yArray.push(value[1]);
});

BTW 2DArray is an invalid variable name. You can't use 2 at the start of a variable.

Also your array declaration is invalid.