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
5
  • you need the merged array to be in that specific order? Commented Oct 20, 2015 at 16:02
  • You can just concat the three arrays. Commented Oct 20, 2015 at 16:04
  • use recursion and while loop, with some control inside the loop iterations that increment over your arrays in the way you seem to be after. Commented Oct 20, 2015 at 16:04
  • Merge/flatten an Array of Arrays in JavaScript? Commented Oct 20, 2015 at 16:04
  • Of course the items I would use will not be the same. But I need the algorithm to merge in that way. Yes. Commented Oct 20, 2015 at 16:05

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

YES! That's exactly what I'm looking for. Thanks @AtheistP3ace!
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)

1 Comment

Howdy. This one is awesome. Thanks!
0

Using lodash you can simpliy do:

_.flattenDeep(_.values(items));

3 Comments

This method do not output the array as I said I need it. Sorry :\
So is the order of the output important ?
Unfortunately yes, I need that exactly output.
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;

1 Comment

Thanks @Mateus! Your answer is even smaller than the other one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.