How to flatten an array in JavaScript