pongs
pongs

Reputation: 53

horizontal to vertical in a javascript array

I have the sample code below;

var myarray= [],
myarray2=[];
var L=100;
for(j =0; j< 4;j++){
    for(i =0; i<4;i++){        
       myarray.push([L]);
       L=L+100;
    }
    myarray2.push(myarray);
    alert(myarray2[j]);
}

The output will be

100,200,300,400,500,600,700,800,900,100,1100,1200,1300,1400,1500,1600

Is it possible to sort the values like below in an array within an array?

100, 500, 900, 1300
200, 600, 1000, 1400
300, 700, 1100, 1500
400, 800, 1200, 1600

I would appreciate any help....thanks in advance.

Upvotes: 0

Views: 10298

Answers (3)

Rocking Rohit
Rocking Rohit

Reputation: 1

You can do this by a one liner using lodash or underscore JavaScript,

let arr=[100,200,300,400,500,600,700,800,900,100,1100,1200,1300,1400,1500,1600];
let len=4;  // Can be varied upon your requirement
let soln= _.unzip( _.chunk(arr,len)).map((x)=>{return (x!==undefined)?x:[];});
console.log(soln); // This will print resultant array

This will print the following result.

[ [ 100, 500, 900, 1300 ],

[ 200, 600, 100, 1400 ],

[ 300, 700, 1100, 1500 ],

[ 400, 800, 1200, 1600 ] ]

Upvotes: 0

Xotic750
Xotic750

Reputation: 23472

This solution will convert from your flat array into the multidimentional array that you have described.

Javascript

var array = "100,200,300,400,500,600,700,800,900,100,1100,1200,1300,1400,1500,1600".split(",");
var grouping = 4;
var result = [];

array.forEach(function (element, index) {
    var group = index % grouping;
    var temp = result[group];

    if (!Array.isArray(temp)) {
        temp = [];
    }

    temp.push(element);
    result[group] = temp;
});

console.log(result);

On jsfiddle

UPDATE:

Ok, I am going to assume that it is myarray, as suggested by @bergi, that you wish to convert and not myarray2. Based on this assumption here is a modified solution

Javascript

var myarray = [],
    myarray2 = [];
var L = 100;
for (var j = 0; j < 4; j++) {
    for (var i = 0; i < 4; i++) {
        myarray.push([L]);
        L = L + 100;
    }

    myarray2.push(myarray);
}

var columns = 4;
var result = [];
var flattened = myarray.toString().split(",");
var length = flattened.length;
var rows = Math.round(length / columns);

flattened.forEach(function (element, index) {
    var group = index % rows;
    var temp = result[group];

    if (!Array.isArray(temp)) {
        temp = [];
    }

    temp.push(element);
    result[group] = temp;
});

console.log(result);

On jsfiddle

Here is an alternative flatten method

var flattened = myarray.reduce(function(a, b) {
    return a.concat(b);
});

if you don't like

var flattened = myarray.toString().split(",");

And for interest only sake, the jsperf on the above flatten methods.

Upvotes: 2

mzedeler
mzedeler

Reputation: 4369

This will generate an array of arrays from scratch.

var x = 10;
var y = 4; 
var result = [];

for(j = 0; j < y; j++) {
  result[j] = [];
  for(i = 0; i < x; i++) {
    result[j].push((j % y + i*y) * 100);
  }
}

console.log(result);

Yields:

[ [   0, 400,  800, 1200, 1600, 2000, 2400, 2800, 3200, 3600 ],
  [ 100, 500,  900, 1300, 1700, 2100, 2500, 2900, 3300, 3700 ],
  [ 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800 ],
  [ 300, 700, 1100, 1500, 1900, 2300, 2700, 3100, 3500, 3900 ] ]

You can adjust the number of entries by changing x and y.

Upvotes: 0

Related Questions