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

5
  • 3
    What issue are you having with the code that you tried? Commented Jan 23, 2018 at 22:20
  • 1
    Possible duplicate of Cartesian product of multiple arrays in JavaScript Commented Jan 23, 2018 at 22:21
  • 1
    What have you tried? This can be done with a number of different loop approaches Commented Jan 23, 2018 at 22:21
  • 1
    is the order in final important Commented Jan 23, 2018 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 Commented Jan 23, 2018 at 22:45

6 Answers 6

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);

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

Solid answer. It's a little messy having to use the new spread syntax (...), but works and doesn't require a third "building array".
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)

11 Comments

why [].concat?
your output does not match desired output :p
Flattens the array so that all items are at the same level. Without it, the result would be a 2-dimensional array: [["PreconEJID", ...], ["ContraEJID", ...], ...]
@JaromandaX I though it did? In what way? OP's final array is not nested. Or do you mean the order is incorrect?
@CRice OP does not specify any particular ordering of the result
|
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);

2 Comments

There should be dashes between the prefix/suffixes
@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.
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)

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.