hemnath mouli
hemnath mouli

Reputation: 2755

Javascript: How to spread a array element to all other element and make a single array

I have a array like

var arr = [["one", "two"],["three", "four"],["five", "six", "seven"],["eight", "nine"]];

I am trying to make all the elements unite the elements, but the elements in the same sub-array must not replicate. like:

var output = ["one three four five six seven eight nine","two three four five six seven eight nine"];

This will be added based on number of elements in the first sub array. I tried it but couldn't find any solution. Can anyone help.?
This is my code i tried :

function big_for( data ){
    var aj = [];
    var k = 0;
    for( var i = 0; i < data.length; i++ ){
        for( var j = 0; j < data[i].length; j++ ){
            aj[k] = data[i][j];
            k++;
        } 
    }
    return arr;
    }

Thanks in advance.!

Upvotes: 1

Views: 620

Answers (1)

Rafael Grillo
Rafael Grillo

Reputation: 232

Sorry @Andy

var arr = [["one", "two"],["three", "four"],["five", "six", "seven"],["eight", "nine"]];

var base = arr.shift();
var result = base.map(function(init) {
    return [].concat.apply([], [init].concat(arr)).join(' ');
});

Upvotes: 6

Related Questions