Nested arrays to single array in javascript -


this silly question but, i´m way new @ javascript. , i´m thinking more should.

i´ll set example:

i have:

animals['cat', 'dog']; 

and,

mood['sad' , 'happy']; 

i want:

animalmood[0]=('cat', 'happy') animalmood[1]=('cat', 'sad') animalmood[2]=('dog', 'happy') animalmood[3]=('dog' 'sad') 

how can achieved?

what correct syntax it? welcome.

use nested for loops, pushing each combination of elements output array.

var animals = ['cat', 'dog'],      mood = ['sad' , 'happy'],      animalsmood = [];    (var = 0; < animals.length; i++)    (var j = 0; j < mood.length; j++)      animalsmood.push([animals[i], mood[j]]);    console.log(animalsmood);

using es6 in node.js, can take more functional approach:

let animals = ['cat', 'dog'],      mood = ['sad' , 'happy'],      animalsmood = [];    animals.foreach(a => mood.foreach(m => animalsmood.push([a, m])));    console.log(animalsmood);


Comments