2

To be more specific is there a simplier way of doing this:

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];

test0.forEach(
    function(item0, index) {
        test1[index].forEach(
            function(item1) {
                item0.push(item1);
            }
        );
    }
);

I have tried to use concat() at the first level of forEach() but it does not work for a reason that still escape me...

Edit: Thanks for the first answers. Bonus question, why this one does not work?

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];

test0.forEach(
    function(item, index) {
        item.concat(test1[index]);
    }
);

Thanks in advance

5
  • Expected result is given by the example above. Commented Aug 14, 2018 at 9:04
  • Should be [[0,2,4,6,8], [1,3,5,7,9,11]] Commented Aug 14, 2018 at 9:05
  • I will suggest you using lodash or underscore here Commented Aug 14, 2018 at 9:11
  • 1
    Second sentence on MDN contains answer to your bonus question. (Note that it's not enough to assign to item but you have to assign result to test0[index]) Commented Aug 14, 2018 at 9:21
  • @barbsan thank you! :) Commented Aug 14, 2018 at 9:28

5 Answers 5

3

enter image description here

Benchmark: https://jsperf.com/merge-array-based-on-index

let test0 = [[0,2,4], [1,3,5]];
let test1 = [[6,8], [7,9,11]];
let result = []
for (let i = 0; i < test0.length; i++) {
	result[i] = [...test0[i], ...test1[i]]
}

console.log(result)

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

2 Comments

Though what you are trying to depict using JSPerf link is true, try to keep operations as similar as possible. Check: jsperf.com/merge-array-based-on-index
Thanks, changed the answer
3

One option is to use .map and spread both arrays, getting the second inner array from the appropriate index in the other larger array:

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];

console.log(test0.map((arr, i) => [...arr, ...test1[i]]));

Comments

3

Use Array.map and Spread Syntax

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];
test0 = test0.map((a,i) => [...a, ...test1[i]]);
console.log(test0);

2 Comments

Your approach is very similar to CertainPerformance's answer. I guess he beat you to it by a minute. :-p
@Rajesh - True. I spent some extra time in getting the links :p And obviously, CertainPerformance understood the problem faster.
1

Solution for an arbitrary count of arrays.

var test0 = [[0, 2, 4], [1, 3, 5]],
    test1 = [[6, 8], [7, 9, 11]],
    result = [test0, test1].reduce((a, b) => a.map((v, i) => [...v, ...b[i]]))

console.log(result);

1 Comment

Looks like you are big fan of reduce function.
1

concat will generate a new array instead of modifying the current array. So in order to make it work, you need to assign the newly generated array back to the parent array on the same index.

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];

test0.forEach(
    function(item, index) {
        test0[index] = item.concat(test1[index]);
    }
);
console.log(test0);

Or you can use map instead.

var test0 = [[0,2,4], [1,3,5]];
var test1 = [[6,8], [7,9,11]];

var mergedArray = test0.map(
    function(item, index) {
        return item.concat(test1[index]);
    }
);
console.log(mergedArray);

ES6 solution

I prefer using concat to spread syntax because of better readability.

let test0 = [[0,2,4], [1,3,5]];
let test1 = [[6,8], [7,9,11]];

console.log(test0.map((arr, i) => arr.concat(test1[i])));

Comments

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.