-1

I have two array ['b','c','d','e'] & ['h','i','k','l','m','n'] I want to find combinations of length 2 for array above using Javascript/AngularJS program. e.g.

['bh','bi','bk','bk','bl','bm','bn','ch,'ci','ck' ...]
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
neha
  • 45
  • 5
  • @AlekseySolovey I think the function map is unnecessary because the function reduce can do that. – Ele Apr 04 '18 at 14:19
  • hi, what would be the values of acc, v, i... i tried using above code in my JS file but see errors – neha Apr 04 '18 at 14:23

2 Answers2

0

Here is functional programming ES6 solution:

var array = ['b','c','d','e'];
var array2 =  ['h','i','k','l','m','n'];

var result = array.reduce( (a, v) =>
    [...a, ...array2.map(x=>v+x)],
[]);
console.log(result);

/*---------OR--------------*/

var result1 = array.reduce( (a, v, i) =>
    a.concat(array2.map( w => v + w )),
[]);
console.log(result1);

/*-------------OR(without arrow function)---------------*/

var result2 = array.reduce(function(a, v, i) {
    a = a.concat(array2.map(function(w){
      return v + w
    }));
    return a;
    },[]
);
console.log(result2);

/*----------for single array------------*/

var result4 = array.reduce( (a, v, i) =>
    [...a, ...array.slice(i+1).map(x=>v+'-'+x)],
[]);
console.log(result4);
yajiv
  • 2,901
  • 2
  • 15
  • 25
  • Thanks, but what would be values of a , v and i. – neha Apr 04 '18 at 14:22
  • @neha try second one – yajiv Apr 04 '18 at 14:25
  • I am getting errors when i used your code in my JS file..do i need to import any files for funnctional programming? I am new to jS. any help apprecriated. I see red marks under "=>" sign – neha Apr 04 '18 at 14:28
  • @neha you are using an older browser that doesn't support an _arrow notation_. `w => v + w` is the same as `function(w){return v + w}` – Aleksey Solovey Apr 04 '18 at 14:31
  • @neha _what would be values of a , v and i._ read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce – yajiv Apr 04 '18 at 14:36
  • Thank you. How can i achieve the same for one single array . e.g I have one array ['b','c','d','e'] , but i want result to be ['b-c','b-d','b-e','c-d'..]; I want to add a hyphen between items. thanks – neha Apr 04 '18 at 14:45
  • Thanks[ "b-b", "b-c", "b-d", "b-e", "c-b", "c-c", "c-d", "c-e", "d-b", "d-c", "d-d", "d-e", "e-b", "e-c", "e-d", "e-e" ] but this has 'b-b','c-c','d-d','e-e' , how can i get rid of those? – neha Apr 04 '18 at 14:57
  • @neha sorry for wrong output, just add `slice` (see in code,updated it) – yajiv Apr 04 '18 at 15:01
  • Why to make so complex: function combinations(a1, a2) { var result = []; a1.forEach(function(v1, id1){ a2.forEach(function(v2, id2){ result.push(v1+v2) }) }) return result; } – Anil Arya Apr 04 '18 at 15:03
0

Use nesting Array.map() calls and combine the letters. Flatten the arrays by spreading into Array.concat():

const arr1 = ['b','c','d','e'];
const arr2 = ['h','i','k','l','m','n'];

const result = [].concat(...arr1.map((l1) => arr2.map((l2) => l1 + l2)));

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209