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?

Michael
  • 25
  • 3

4 Answers4

4

Try using reduce:

arr.reduce((a, e) => a.concat(e))
guijob
  • 4,413
  • 3
  • 20
  • 39
  • 1
    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))` – Ates Goral Jul 15 '17 at 02:41
  • 1
    thank you for your advices! i've just made an edit to implement this tips. – guijob Jul 15 '17 at 03:04
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);
guest271314
  • 1
  • 15
  • 104
  • 177
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.

nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thank you! I had everything except for the the assignment of `joined` to the concat method. – Michael Jul 15 '17 at 02:42
  • @Michael glad to help mate :) – nem035 Jul 15 '17 at 02:42
  • With rest and spread: `var joinArrayOfArrays = (...args)=>[].concat(...args);`. ;-) – RobG Jul 15 '17 at 03:02
  • 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. – Michael Jul 15 '17 at 03:15
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"]
  • Thanks! This particular problem was asking specifically for the concat method but I'll definitely do that. – Michael Jul 15 '17 at 06:16