How to concat arrays alternatively every 2 items? JS

Aerra

I have:

array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
array2 = ['a', 'b', 'c'];

I got the concat of arrays working, but only one at a time:

var alternatingArrayResult = [array1, array2].reduce(function (r, a) {
                        return a.forEach(function (a, i) {
                            return (r[i] = r[i] || []).push(a);
                        }), r;
                    }, []).reduce(function (a, b) {
                        return a.concat(b);
                    });

// => alternatingArrayResult = [1, 'a', 2, 'b', 3, 'c', 4, 5, 6, 7, 8, 9];

I want to add two items from array 1, then one from array 2 - and so on.

Desired output:

result = [1, 2, 'a', 3, 4, 'b', 5, 6, 'c', 7, 8, 9];

var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var array2 = ['a', 'b', 'c'];
var alternatingResultArray = [array1, array2].reduce(function (r, a) {
                    return a.forEach(function (a, i) {
                        return (r[i] = r[i] || []).push(a);
                    }), r;
                }, []).reduce(function (a, b) {
                    return a.concat(b);
                });
                    
console.log(alternatingResultArray);

Nina Scholz

You could get a lenght and iterate the parts for pushing.

const
    array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9],
    array2 = ['a', 'b', 'c'],
    result = [];

for (i = 0, l = Math.max(Math.ceil(array1.length / 2), array2.length); i < l; i++) {
    result.push(
        ...array1.slice(i * 2, (i + 1) * 2),
        ...array2.slice(i, i + 1),
    );
}

console.log(...result);

A more abstact version with size of the wanted subarrays.

const
    array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9],
    array2 = ['a', 'b', 'c'],
    data = [array1, array2],
    sizes = [2, 1]
    result = [];

for (i = 0, l = Math.max(...data.map(({ length }, i) => Math.ceil(length / sizes[i]))); i < l; i++) {
    data.forEach((a, j) => result.push(...a.slice(i * sizes[j], (i + 1) * sizes[j])));
}

console.log(...result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

concat 2 objects with arrays and delete duplicates (js)

From Dev

How to merge 2 columns in a file alternatively?

From Dev

How to compare 2 arrays for common items in PHP?

From Dev

How to concat php arrays

From Dev

Need to concat arrays( don't know how much would be) (js)

From Dev

How can i get a unique id for every class items in this js

From Dev

How to write 2 lists of items in 2 columns instead of 2 arrays?

From Dev

How to concat two subclassed arrays?

From Dev

Interleave 2 strings alternatively in C

From Dev

2 arrays, find every permutation

From Dev

comparing 2 arrays in every position

From Dev

Concat two arrays retuning circular js

From Dev

how to alternatively concatenate 3 strings

From Dev

how to alternatively concatenate 3 strings

From Dev

Relating items between 2 arrays

From Dev

Concat 2D Arrays in f#

From Dev

Concat 2D Arrays in f#

From Dev

Compare each and every items of two arrays C#

From Dev

How to concat two string arrays in Java

From Dev

How to merge two arrays and concat the values with PHP

From Dev

Moving items between arrays in vue.js

From Dev

How to iterate a loop every n items

From Dev

javascript for loop how to count every four items?

From Dev

javascript for loop how to count every four items?

From Dev

How to iterate a loop every n items

From Dev

how to alternatively resolve and reject jquery object

From Dev

How to check characters alternatively and replace it with Y if it is X?

From Dev

How to alternatively change BackColor of RTB text

From Dev

How to resize and alternatively shift+resize?