0

I have two arrays, for example:

Array1:

arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]

Array2:

arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

I want to generate a new array from two above concatenating the individual items into new items recursively for each one in array1, to get a final array like this:

final = ['Precon-EJID', 'Contra-EJID', 'Postco-EJID', 'Cancel-EJID', 'Consul-EJID', 'Precon-EMBA', 'Contra-EMBA', 'Postco-EMBA', 'Cancel-EMBA', 'Consul-EMBA', 'Precon-EMPR', 'Contra-EMPR', 'Postco-EMPR', 'Cancel-EMPR', 'Consul-EMPR'...etc]

Thank you in advance

Mark Hkr
  • 620
  • 2
  • 7
  • 15
  • 3
    What issue are you having with the code that you tried? – guest271314 Jan 23 '18 at 22:20
  • 1
    Possible duplicate of [Cartesian product of multiple arrays in JavaScript](https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript) – pwolaq Jan 23 '18 at 22:21
  • 1
    What have you tried? This can be done with a number of different loop approaches – charlietfl Jan 23 '18 at 22:21
  • 1
    is the order in `final` important – Jaromanda X Jan 23 '18 at 22:30
  • 1
    The multiple answers posted to this Question prove that the "downvote" and "close" vote standards and rationales, if any, are fully arbitrary and capricious. Which is ok. Just be aware that it is duly noted if any of the users whom answered this Question find the audacity to post a comment questioning this users' answering any question here at SO whatsoever. None of the users whom posted answers to this question should vote to close another question under the premise of "off-topic because -> ("why isn't this code working?")" else that vote is fraudulent even if only you know that fact – guest271314 Jan 23 '18 at 22:45

6 Answers6

4

You can do this with 2 simple for of loops:

var arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"];

var arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

var finalArr = [];

for ( var item2 of arr2 ) {
  for ( var item1 of arr1 ) {
    finalArr.push(`${item1}-${item2}`);
  }
}

console.log(finalArr);
Blue
  • 22,608
  • 7
  • 62
  • 92
2

You can use nested Array#map calls, and flatten the results using Array#concat:

const arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]

const arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

const result = [].concat(...arr2.map((s1) => arr1.map((s2) => `${s2}-${s1}`)))

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Solid answer. It's a little messy having to use the new spread syntax (`...`), but works and doesn't require a third "building array". – Blue Jan 24 '18 at 06:03
0

Here's a one liner to do this:

arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]
arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

const result = [].concat(...arr1.map(prefix => arr2.map(suffix => prefix+suffix)));
console.log(result)

// EDIT: if order matters, reverse the use of arr1 and arr2:

const orderedResult = [].concat(...arr2.map(suffix => arr1.map(prefix => prefix+suffix)));
console.log(orderedResult)
CRice
  • 29,968
  • 4
  • 57
  • 70
0

const prefixes = [
  "Precon",
  "Contra",
  "Postco",
  "Cancel",
  "Consul"
];

const suffixes = [
  "EJID", "EMBA", "EMPR",
  "GOBI", "PART", "PPOL",
  "SACI", "SOFL", "SOFM",
  "0000", "", "0002",
  "0003", "0004", "0005",
  "0006", "0007", "0008",
  "0009", "0010", "0011",
  "0012", "0013", "0014",
  "0015", "0016", "011",
  "0110", "9999"
];

const results = suffixes
  .map(suffix => prefixes.map(prefix => prefix + '-' + suffix))
  .reduce((xs, ys) => [...xs, ...ys]);

console.log(results);
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • 1
    There should be dashes between the prefix/suffixes – Blue Jan 23 '18 at 22:39
  • @FrankerZ If we are positing what "should be". There should first be a modicum of effort put forth by OP to resolve their own inquiry, which is wholly absent from the Question. – guest271314 Jan 23 '18 at 22:40
0

Another option is to use reduce...

let arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]

let arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

let arr3 = arr2.reduce((arr, val) => {
    let f = []
    arr1.forEach(itm => val && f.push(itm + '-' + val))
    return arr.concat(f)
}, [])

console.log(arr3)
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

Here's a recursive one. Just like the OP asked. Took me a while. :P

arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"]

arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"]

var arr = [];

console.log(concat(arr1, arr2, 0, 0, 0));

function concat(arr1, arr2, arrIndex, index1, index2) {
  //console.log(arr1.length);
  if (index2 === (arr2.length)) {
    return;
  } else {
    arr[arrIndex] = arr1[index1] + '-' + arr2[index2];
    if (index1 !== (arr1.length - 1)) {
      index1++;
    } else if (index1 === (arr1.length - 1)) {
      index1 = 0;
      index2++;
      //concat(arr1, arr2, ++arrIndex, index1, index2++); // Not here dummy :P
    }
    concat(arr1, arr2, ++arrIndex, index1, index2++);
  }
  return arr;
}
Jimenemex
  • 3,104
  • 3
  • 24
  • 56