hanki.dev

JavaScript has a method to flatten nested arrays

Pretty basic, but I just found out js has an awesome .flat() method. Example:

const arr = [ 1, [ 2, 3 ], 4, [ ] ]
console.log(arr)                      // Output: [ 1, [ 2, 3 ], 4, [] ]
console.log(arr.flat())               // Output: [ 1, 2, 3, 4 ]

MDN: Array.prototype.flat()

#javascript