horizontal to vertical in a javascript array

javascript transpose array of objects
how to transpose 2d array javascript
javascript pivot array
how to rotate 2d array javascript
convert array to string javascript
lodash pivot
javascript rotate array of arrays
javascript 2d 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.

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.

Pivot a JavaScript Array: Convert a Column to a Row, 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; }� TRANSPOSE simply switches an array from vertical to horizontal (and vice versa), but depending on what you are trying to do, you'll get different results as per the first part of my answer, so you'll either have N/A when excel cannot match an item of an array with another item of the other array, or 'expansion' of the two arrays that results in a bigger array.

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.

PHP array_column() Function, In this post, we will create a method which converts a specified column to row in JavaScript array like below: Consider the following array: System.out.println(Arrays.toString(i1)); – Paul Boddington Sep 9 '15 at 22:09 I made an edit, now I have 3 arrays. They numbers print horizontally now, but the 2nd and 3rd set of arrays are also on the same horizontal line. – agentmg123 Sep 9 '15 at 22:14

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 ] ]

element.scrollLeft, The array_column() function returns the values from a single column in the input array. Syntax. array_column(array, column_key, index_key). Parameter Values� It is OK if you want to call a 2D array with shape, (4, 1), a "vertical" array. Then a 2D array with shape, (1, 4), would be a "horizontal" array. In that case the transpose will work as you expect it should. The transpose a[:, np.newaxis].T gives a horizontal array with shape, (1, 4). You can always check the shape of arrays with print(a.shape).

Nested object data (arrays), Get the number of pixels the content of a <div> element is scrolled horizontally and vertically: var elmnt = document.getElementById("myDIV"); var x = elmnt. Convert horizontal to vertical with formula. Here I introduce a formula can help you convert horizontal list to vertical list in Excel. Select a blank cell and type this formula =INDEX(A$1:L$1,ROWS(M$1:M1)) into it, and press Enter key, then drag the fill handle down till a wrong value appears.

For loop with vertical vs horizontal array, The Javascript shown below is used to initialise the table shown in this example: $(document).ready(function() { $('#example').DataTable( { "ajax": "data/� The call to new Array(number) creates an array with the given length, but without elements. The length property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods. If we shorten length manually, the array is truncated. We can use an array as a deque with the following operations:

Excel formula: Transpose table without zeros, For loop with vertical vs horizontal array. Learn more about for loop, for, linear indexing. converting a horizontal matrix to vertical. Learn more about matrix, horizontal to vertical, find peak Signal Processing Toolbox

Comments
  • Is myarray2 really supposed to contain 4 references to the same myarray?
  • Are you wanting to create the array in that format or alter the flat array to that format?
  • Why don't you create that 2d-array you want as result in the first place?
  • I need just a little more information. Are you looking to achieve is in an alert message of would you be open to an HTML approach. Also are you trying to arrang these in colums that are no longer than 4 or in no more than 4 columns no matter the length?
  • I just made an example of 4..it can be dynamic Bergi.
  • Actually, his myArray is [[100],[200],[300],[400],[500],[600],[700],[800],[900],[1000],[1100],[1200],[1300],[1400],[1500],[1600]] - that array string is only the output if you alert it.
  • Aha, thanks, I didn't look at what he actually created, just what he stated.