Search code examples
javascriptes6-promise

Rewrite concat function in es6


I have a simple function which I am trying to rewrite in es6 but I am not getting it

Where arr1 and arr2 are multidimensional arrays of different widths i.e. arr1[0].length != arr2[0].length but do have the same lengths

function conatArrAsColumns(arr1, arr2) {
  
//This works
  var result = arr1.map(function(row, i){
  return row.concat(arr2[i]);
  });
  
  
//This works
  var result = arr1.map((row, i) => {
  return row.concat(arr2[i]);
  });
  
  
 //This does not work
  var result = arr1.map((row) => {
  return row.concat(arr2);
  });
  

//This does not work
  var result = arr1.map(row => row.concat(arr2)));

  return result
}

I am trying to get this to work

var result = arr1.map(row => row.concat(arr2)));

Thank you for any assistance with this


Solution

  • Your attempts that "work" all use the index, whereas the ones that don't "work" do not. Your last example that you are "trying to get this to work", is correct - you're just missing the index:

    var result = arr1.map((row, i) => row.concat(arr2[i]));