admin管理员组

文章数量:1024639

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.

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.

Share Improve this question edited May 2, 2013 at 21:50 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked May 2, 2013 at 21:44 pongspongs 531 gold badge1 silver badge7 bronze badges 9
  • Is myarray2 really supposed to contain 4 references to the same myarray? – Bergi Commented May 2, 2013 at 21:47
  • Are you wanting to create the array in that format or alter the flat array to that format? – Xotic750 Commented May 2, 2013 at 21:47
  • Why don't you create that 2d-array you want as result in the first place? – Bergi Commented May 2, 2013 at 21:48
  • 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? – Jason Sperske Commented May 2, 2013 at 21:49
  • I just made an example of 4..it can be dynamic Bergi. – pongs Commented May 2, 2013 at 21:49
 |  Show 4 more ments

3 Answers 3

Reset to default 2

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.

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.

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

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.

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.

Share Improve this question edited May 2, 2013 at 21:50 Bergi 667k161 gold badges1k silver badges1.5k bronze badges asked May 2, 2013 at 21:44 pongspongs 531 gold badge1 silver badge7 bronze badges 9
  • Is myarray2 really supposed to contain 4 references to the same myarray? – Bergi Commented May 2, 2013 at 21:47
  • Are you wanting to create the array in that format or alter the flat array to that format? – Xotic750 Commented May 2, 2013 at 21:47
  • Why don't you create that 2d-array you want as result in the first place? – Bergi Commented May 2, 2013 at 21:48
  • 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? – Jason Sperske Commented May 2, 2013 at 21:49
  • I just made an example of 4..it can be dynamic Bergi. – pongs Commented May 2, 2013 at 21:49
 |  Show 4 more ments

3 Answers 3

Reset to default 2

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.

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.

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

本文标签: horizontal to vertical in a javascript arrayStack Overflow