-1

I have N number of arrays in an array, for now N = 3

[[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8]...]

I would like for it to return

[[1,3,6], [2,4,7], [3,5,8], ...., ["a","c", ""], ["b", "",""]]

There are lot of solutions of combining two arrays but I want to handle any number of arrays. I would like for it to return above. I am looking to do this without underscore or jquery.

    Values=[];
    status = [[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8]];
    status_name = status[0]; //take the longest one always
    for (i = 0; i < status_name.length; ++i)
    {
        Values[i] =Array(status_name[i], status[i+1][i]);
    }
user299709
  • 4,922
  • 10
  • 56
  • 88

3 Answers3

0

Conceptually, this is most easily achieved using nested loops;

var a = [[1, 2, 'x'], ['a', 'b'], ['+', '-', 'y', 'z']]; // input array

function transform(a) {
    var i, j, // two loops = two variables to iterate with
        foundSomething, // a flag so we know to stop
        b = []; // output array

    i = 0;
    do { // loop for i
        foundSomething = false;
        b[i] = [];
        for (j = 0; j < a.length; ++j) { // loop for j
            if (i in a[j]) { // something exists at this level of i
                foundSomething = true;
                b[i].push(a[j][i]);
            } else { // nothing here
                b[i].push(''); // insert empty string
            }
        }
        ++i;
    } while (foundSomething);
    b.length = b.length - 1; // snip empty end
    return b;
}

transform(a); // [[1, "a", "+"], [2, "b", "-"], ["x", "", "y"], ["", "", "z"]]

Sorting required?

a.sort(function (a, b) {return a.length < b.length;}); // bigger arrays first
transform(a); // [["+", 1, "a"], ["-", 2, "b"], ["y", "x", ""], ["z", "", ""]]

If you're always doing this sort, you can further optimise transform i.e. because you know that a[x].length >= a[y].length for all x < y, you don't need to flag whether you found something

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • this is pretty close the output is not quite what I expected. IT should always take the longest array as the primary item, and if an array is missing that nth item, it needs to be an empty string "". `[+,1,a], [-,2,b], [y,x,''], [z, '','']` – user299709 Jul 25 '14 at 23:32
  • You will need to sort your array by the `.length` of it's items (look up [`Array.prototype.sort`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)); if you're doing this already then this code works, if you can assume that it's sorted then there will be other optimisations you can make, e.g. using a `for` instead of the `do..while` with a flag as you know the maximum depth from the first item's length, cutting the loop short when you know there will be no others that have the item, etc – Paul S. Jul 25 '14 at 23:36
  • @downvoter please comment so I might address the issue you have with this answer – Paul S. Jul 25 '14 at 23:48
0

why not nest loops ?

sth. like this warning pseudocode

Values=[];
status = [[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8];
status_name = status[0]; 


for (i = 0; i < status.length; ++i)
{
    for (k = 0; k < status[i].length; ++i)
    {
        Values[i] =Array(status[i][k], status[k+1][k]);
    }

}
john Smith
  • 17,409
  • 11
  • 76
  • 117
0
var originalArray = [[1, 2, 3, "a", "b"], [3, 4, 5, "c"], [6, 7, 8]];
var theValues = new Array();
var actualArray = originalArray.pop();
var maxLength = originalArray.length;
while (actualArray) {
  for (var i = 0; i < actualArray.length; i++){
    var newArray = theValues[i];
    if (!newArray) {
      theValues[i] = new Array();
    }
    theValues[i].push(actualArray[i])
  }
  actualArray = originalArray.pop();
}
for (var i = 0; i < theValues.length; i++) {
  for (var j = maxLength - theValues[i].length; j >= 0; j--) {
    theValues[i].push("");
  }
}
Aguardientico
  • 7,641
  • 1
  • 33
  • 33