4

I am looking for a JavaScript function which accepts two string arrays of equal length and outputs a single string array which is the same length as the input arrays, containing the element-wise-concatenated strings of the input arrays. Is there a built-in JavaScript function which does this?

Additionally, I would like to add in a string between the concatenated elements when the element-wise concatenation is done. For example, so that this would be true for each i:

outputArray[i] = inputArray1[i] + " - " + inputArray2[i]
4
  • 2
    From the sample you posted you already have a solution that would work, if you are looping over the input array Commented Aug 2, 2018 at 18:58
  • I'm looking for a function though Commented Aug 2, 2018 at 19:05
  • 1
    Step 1: Take the code you have working now and move it into a function Step 2: call that function Step 3: Profit Commented Aug 2, 2018 at 19:06
  • There's no in-built one to perform specifically this action. It's known as a zip. The closest thing in the js standard library is a reduce like Nina has posted. There's no reason why you couldn't make your own zip function. Commented Aug 2, 2018 at 19:12

1 Answer 1

7

You could reduce an array with the single arrays. This works for more than one array as well.

var inputArray1 = ['abc', 'def', 'ghi'],
    inputArray2 = ['3', '6', '9'],
    outputArray = [inputArray1, inputArray2].reduce((a, b) => a.map((v, i) => v + ' - ' + b[i]));

console.log(outputArray);

More functional

var inputArray1 = ['abc', 'def', 'ghi'],
    inputArray2 = ['3', '6', '9'],
    outputArray = [inputArray1, inputArray2]
        .reduce((a, b) => a.map((v, i) => [].concat(v, b[i]))) // get single parts
        .map(a => a.join(' - '));                              // join inner arrays

console.log(outputArray);

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

5 Comments

He asked for a function so: concatStrings = (a, b) => { return [a, b].reduce((a, b) => a.map((v, i) => v + '-' + b[i])); }
That's exactly what I'm looking for! Thanks. My primary need was for a one-liner that would be easy to use in the Chrome Dev Tools javascript console
@Marathon55 just an FYI you can run multiline scripts in Chrome Dev Tools Console, I do it all the time. This is unrelated to your question or answer but just a little tidbit I thought I would pass on.
Is there any specific reason why we need the .reduce() as well? In my opinion, the function itself can work simply with the map, right? Like merge = (a,b) => {return a.map(val, ind) => val + "-" + b[ind]);}
right, with map it would work to, but it needs to address the other array inside of the callback. a better approach is to collect all necessary array in another array and get the result from this array, because it it easily to extend with more arrays,

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.