_.concat lodash vs Array.prototype.concat()

Viewed 820

what is the difference between _.concat lodash and Array.prototype.concat() ?.

1 Answers

welcome to Stackoverflow!

The functions work the same way, but the syntax is different:

_.concat ->

let array1 = [1, 2, 3];
let array2 = [4, 5, 6],
let array3 = [7, 8, 9];
let allArrays = _.concat(array1 , array2 , array3);
console.log(allArrays ); // [1,2,3,4,5,6,7,8,9]

array.concat ->

let array1 = [1, 2, 3];
let array2 = [4, 5, 6],
let array3 = [7, 8, 9];
let allArrays = [].concat(array1 , array2 , array3);
console.log(allArrays ); // [1,2,3,4,5,6,7,8,9]

or array.concat ->

let array1 = [1, 2, 3];
let array2 = [4, 5, 6],
let array3 = [7, 8, 9];
let allArrays = [];
allArrays.concat(array1 , array2 , array3);
console.log(allArrays ); // [1,2,3,4,5,6,7,8,9]

basically the array.concat is called from ARRAYs and _.concat doesn't need an array to be called

Related