Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is alternative way concat in js

I'm having a problem with my function flatten

Here's the code:

    function flatten(arrays) {
    return [].concat.apply([], arrays);
}

There's an error of max exceed so i tried to change the function to this:

    function flatten(arrays){
    return arrays.reduce(function(prev, curr){
        return prev.concat(curr);
    }, []);
}

the error was gone but its too slow. Is there alternative code for concat.

like image 713
Kel Avatar asked Nov 21 '25 05:11

Kel


1 Answers

Try this ES6 spread syntax:

const array1 = [1,2,3]
const array2 = [4,5,6]
const array3 = [...array1, ...array2]
console.log(array3)
like image 114
Developer Nodejs Avatar answered Nov 22 '25 17:11

Developer Nodejs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!