-1

This could be a silly question but, I´m way too new at javascript. And i´m thinking this more than it should.

I´ll set an 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 this be achieved?

What would be the correct syntax for it? Any help is welcome.

Karma Doe
  • 114
  • 12
  • What format is this `animalmood[0]=('Cat', 'Happy')`? Do you want it as a string `animalmood[0]='Cat, Happy';` or as an array `animalmood[0]=['Cat', 'Happy']`? – Mohit Bhardwaj Jul 21 '16 at 17:06
  • Possible duplicate of [Cartesian product of multiple arrays in JavaScript](http://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript) – 4castle Jul 21 '16 at 17:11

2 Answers2

2

Use nested for loops, pushing each combination of elements into the output array.

var animals = ['Cat', 'Dog'],
    mood = ['Sad' , 'Happy'],
    animalsMood = [];

for (var i = 0; i < animals.length; i++)
  for (var j = 0; j < mood.length; j++)
    animalsMood.push([animals[i], mood[j]]);

console.log(animalsMood);

Using ES6 in Node.js, you can also take a more functional approach:

let animals = ['Cat', 'Dog'],
    mood = ['Sad' , 'Happy'],
    animalsMood = [];

animals.forEach(a => mood.forEach(m => animalsMood.push([a, m])));

console.log(animalsMood);
4castle
  • 32,613
  • 11
  • 69
  • 106
2

A more advanced answer is using reduce function:

const animals = ['Cat', 'Dog'];
const moods = ['Sad' , 'Happy'];


const animalsMood = animals.reduce((result, animal) => 
  result.concat(moods.map(mood => [animal, mood]))
, []);

console.log(JSON.stringify(animalsMood));

Using spread instead of concat

const animals = ['Cat', 'Dog'];
const moods = ['Sad' , 'Happy'];


const animalsMood = animals.reduce((result, animal) => 
  [...result, ...moods.map(mood => [animal, mood])]
, []);

console.log(JSON.stringify(animalsMood));
anvk
  • 1,183
  • 8
  • 10
  • This isn't more advanced, this is just a different programming style. It's functional rather than imperative. I really wish JavaScript had a `.collect()` method instead of forcing us to use `.reduce()` or some other non-traditional function. – 4castle Jul 21 '16 at 17:35
  • `reduce` or `map` functions are traditional functions which meet all "pure function" criterias of the functional language such as JavaScript. Using and understanding `reduce` will make you use and understand complex operations in NoSQL databases or understanding one of the main concepts of Reducers in `Redux` or ... many other advanced logic constructs or operations – anvk Jul 21 '16 at 17:46
  • I would also suggest you to read.... another StackOverflow question (since we are using StackOverflow in the end) http://stackoverflow.com/a/388329/812519 maybe it will give you some ideas – anvk Jul 21 '16 at 17:47
  • I just meant that `reduce` is non-traditional when it comes to producing an array. Collecting is a type of reduction, but it would be nice if there wasn't so much boilerplate when using `reduce` in order to `collect`. Purely functional languages have much better alternatives. – 4castle Jul 21 '16 at 17:54