-4

I want to merge some arrays in a particular way. What it looks like now:

[key1, key2, key3, key4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]
[val1, val2, val3, val4]

How i want it to look in de end is

[key1, val1, val1, val1, val1]
[key2, val2, val2, val2, val2]
[key3, val3, val3, val3, val3]
[key4, val4, val4, val4, val4]

Hope you guys have a solution for me thanks!

PS: If something isn't clear enough just comment.

  • `key1` for all arrays in the output? Not `key2`, `key3` etc? – Andy Feb 16 '16 at 10:43
  • 2
    Also, add the code you've tried already to your question. – Andy Feb 16 '16 at 10:45
  • Oops you're right, it should be key2,key3 etc – Leroy Steding Feb 16 '16 at 10:48
  • The code i have right now just returns the current output. And i can't turn it into the wanted output. Can't figure out how.. – Leroy Steding Feb 16 '16 at 10:51
  • What is key1, key 2, key3, key3? are they strings? They are not defined. – RunningFromShia Feb 16 '16 at 10:52
  • Al of the values including the key are strings, i just need them in a particular order. This can be the wanted output for example: ` [webClient.title, title, titel, tietel]` – Leroy Steding Feb 16 '16 at 10:55
  • We understand that your current code doesn't do what you want. But your question would be _much_ better if it contained _some_ relevant code. That gives people a starting-point for their answers and it also gives us an idea of your skill level, which makes it easier for us to write explanations that you'll understand. Please see [mcve]. – PM 2Ring Feb 16 '16 at 11:05

2 Answers2

0

Just change row and column. Basically this: r[j][i] = x[i][j]

var data = [['key1', 'key2', 'key3', 'key4'], ['val11', 'val21', 'val31', 'val41'], ['val12', 'val22', 'val32', 'val42'], ['val13', 'val23', 'val33', 'val43'], ['val14', 'val24', 'val34', 'val44']],
    pivot = function (data) {
        var r = [];
        data.forEach(function (a, i) {
            a.forEach(function (b, j) {
                r[j] = r[j] || [];
                r[j][i] = b;
            });
        });
        return r;
    }(data);

document.write('<pre>' + JSON.stringify(pivot, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This could be the solution, but the thing is the arrays are dynamic so it hasn't have a fixed index. For example key has 30 values and the value arrays have also 30 values will it still work then? – Leroy Steding Feb 16 '16 at 11:42
  • @LeroySteding, why not, try it with your data. – Nina Scholz Feb 16 '16 at 11:50
  • It seems like the data isn't ordered correctly some of the values are on places where they do not belong haha im looking in to it right now! – Leroy Steding Feb 16 '16 at 12:12
0

You can create an array of these arrays and fill your new arrays like below :

var array1 = ["key1", "key2", "key3", "key4"];
var array2 = ["val1", "val2", "val3", "val4"];
var array3 = ["val1", "val2", "val3", "val4"];
var array4 = ["val1", "val2", "val3", "val4"];
var array5 = ["val1", "val2", "val3", "val4"];

allArrays = [array1,array2,array3,array4,array5];
array11 = []
array22 = []
array33 = []
array44 = []
for(var i = 0; i < allArrays.length; i++){
  array11.push(allArrays[i][0]);
  array22.push(allArrays[i][1]);
  array33.push(allArrays[i][2]);
  array44.push(allArrays[i][3]);

}

https://jsfiddle.net/fh1ou9c2/

AshBringer
  • 2,614
  • 2
  • 20
  • 42