1

I have a problem that asks me to join arrays of an array and return a single array in the form of [ array[0][0], array[0][1], array[1][0], array[1][1], etc. ]. I solved it using the push method in nested for-loops, but the prompt says that I should be familiar with the concat method. I know the concat method syntax and how it works, but I can't figure out how to use it to do what the prompt asks for.

Here's my solution using the push method:

function joinArrayOfArrays(arr) {
  var joined = [];
  for (var i = 0; i < arr.length; i++) {
    for (var k = 0; k < arr[i].length; k++) {
      joined.push(arr[i][k]);
    }
  } 
  return joined;
}

joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']]);

// => [ 1, 4, true, false, 'x', 'y' ]

How would I return the same output using the concat method?

4 Answers 4

4

Try using reduce:

arr.reduce((a, e) => a.concat(e))
Sign up to request clarification or add additional context in comments.

2 Comments

No need for the second [] argument. When you omit it, it means you want to use the first item in the array as the starting value. Also, a little white space doesn't hurt anyone: arr.reduce((a, e) => a.concat(e))
thank you for your advices! i've just made an edit to implement this tips.
2

You can use spread element preceding array as parameter to .concat()

let res = [].concat(...[[1, 4], [true, false], ['x', 'y']]);

console.log(res);

Using a function

const joinArrayOfArrays = (arrays = []) => [].concat(...arrays);

let res = joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']]);

console.log(res);

Comments

1

If you want to use concatenation, you wouldn't need the second loop, you can just concatenate each sub array within a single loop. One thing to remember is that concat doesn't modify exist array but returns a new one:

function joinArrayOfArrays(arr) {
  var joined = [];
  for (var i = 0; i < arr.length; i++) {
    joined = joined.concat(arr[i]) // concat sub array
  } 
  return joined;
}

console.log(
  joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']])
);

You can also use a spread in a similar manner:

function joinArrayOfArrays(arr) {
  var joined = [];
  for (var i = 0; i < arr.length; i++) {
    joined = [...joined, ...arr[i]] // spread
  } 
  return joined;
}

console.log(
  joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']])
);

Using reduce, as suggested by J. Guilherme, is a more elegant way to do this.

4 Comments

Thank you! I had everything except for the the assignment of joined to the concat method.
@Michael glad to help mate :)
With rest and spread: var joinArrayOfArrays = (...args)=>[].concat(...args);. ;-)
Thanks, I'm new. Now I see that the reason I couldn't get the solution using array concatenation is that I was assuming concat modified the existing array. So thanks also for that key piece of information.
0

If you are trying to practice your array methods, I suggest looking up the reduce method on MDN and adapt this solution to this problem as an alternative to a for-loop:

function joinArrayOfArrays(arr) {
      return arr.reduce(function(a, b) {
        return a.concat(b);
      }); 
    }

    var output = joinArrayOfArrays([[1, 4], [true, false], ['x', 'y']]);
    console.log(output); 
    // [1, 4, true, false, "x", "y"]

1 Comment

Thanks! This particular problem was asking specifically for the concat method but I'll definitely do that.

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.