0

So I have this issue.

I have this object containing 3 arrays...

items = {
    a: ['STRAWBERRY', 'PEANUT'],
    b: ['John', 'Doe', 'Scarface'],
    c: ['Circle', 'Square', 'Triangle', 'Rectangle'],
}

I need this output:

[
    'STRAWBERRY',
    'John',
    'Circle',
    'PEANUT',
    'Doe',
    'Square',
    'Scarface',
    'Triangle',
    'Rectangle'
]

NOTE THAT THE OUTPUT INTERLEAVES BETWEEN EACH VALUE OF EACH ARRAY OF THE 'ITEMS' OBJECT

I tried to figure out a good way to do that kind of "merge" but I can only think in giant methods with tons and tons of code and whiles.

Is there a fast and optimized way to do this?

P.S.: I can use any framework.

EDIT Using @evillive answer I was able to adapt to my code in Coffee Script

RESOLUTION USING COFFEE SCRIPT

result = []
col = -1
loop
  col++
  run = false
  for k of Items
    Items[k][col] and (run = result.push(Items[k][col]))
  unless run
    break

console.log result

4 Answers4

1

To preserve that order you can do this: http://jsfiddle.net/o5k965ze/

var items = {
    a: ['STRAWBERRY', 'PEANUT'],
    b: ['John', 'Doe', 'Scarface'],
    c: ['Circle', 'Square', 'Triangle', 'Rectangle'],
};

function getLength (obj) {
    var length = 0;
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            if (obj[key].length > length) {
                length = obj[key].length;
            }
        }
    }
    return length;
}

function merge () {
    var newArray = [];
    var length = getLength(items);
    console.log(length);
    var index = 0;
    for ( ; index < length; index++) {
        for (var key in items) {
            if (items[key][index]) {
                newArray.push(items[key][index]);
            }
        }
    }
    console.log(newArray)
}

merge();

This will give you order you want and should work for having more than 3 arrays in that object.

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
0

Using lodash you can simpliy do:

_.flattenDeep(_.values(items));
orbatschow
  • 1,497
  • 2
  • 13
  • 26
0
var Items = {
  a: ['STRAWBERRY', 'PEANUT'],
  b: ['John', 'Doe', 'Scarface'],
  c: ['Circle', 'Square', 'Triangle', 'Rectangle'],
};
var length = 0;
var index = 0;
var output = [];

for (var i in Items) {
  length = Math.max(length, Items[i].length);
}

if (!length) return output;

for (index; index < length; index++) {
  for (var i in Items) {
    var item = Items[i][index];
    item && output.push(item);
  }
}

return output;
0

Without needing for lookup max length param.

var result = [];
while(Object.getOwnPropertyNames(items).length){
  for(var k in items){
    result.push(items[k].shift());
    if (items[k].length == 0) delete items[k];
  }
}

UPDATE: Unfortunately shift and delete instructions make previous code insufficient in the sense of performance. So I rewrote it to make it much faster.

var result = [], col = -1, run;
do {
    col++; run = false;
    for(var k in items){
        items[k][col] && (run = result.push(items[k][col]))
    }
} while(run)
evilive
  • 1,781
  • 14
  • 20