Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I just did this method to not use reduce, concat or any kind of special method from JS and trying to have the
- // cheapest complexity.
- function flattenArray(array) {
- let i = 0;
- while (i != array.length) {
- let valueOrArray = array[i];
- if (! Array.isArray(valueOrArray)) {
- i++;
- } else {
- array.splice(i, 1, ...valueOrArray);
- }
- }
- return array;
- }
- function test_flatten_array(){
- let arr1 = [[1,3,[99]],41];
- let arr2 = [1,[9,[5,[3],52,],8],2];
- console.log('Flattened array 1:', flattenArray(arr1));
- console.log('Flattened array 2:', flattenArray(arr2));
- }
- test_flatten_array();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement