For simplicity i have used JS, and i am fully aware that
flatArray()
will give array of string, insted a array of numbers. Since js is loosely typed, it's okay to save like this. You can useflatIntigerArray()
to get array as Integer array, i am supposing that i will receive intger nested array as said.
- Please open browser (I am using Chrome)
- Press CTRL+SHIFT+J
- Paste below code to see output
function flatArray(array){
return JSON.stringify(array).replace(/[\[\]]/g,'').split(",");
}
nestedArray = [[1,2,[3]],4];
flatArray(nestedArray)
function flatIntigerArray(array){
return JSON.stringify(array).replace(/[\[\]]/g,'').split(",").map(Number);
}