1

The question I have been given is;

Given a nested array or arrays, return a new, flat array with all the elements of all the nested arrays in their original order

The answer that I have come up with is;

function flattenArray (arrayOfArrays) {  
arrays = arrays.reduce(function(a, b){
    return a.concat(b);
}, []);

console.log(merged);

My answer is being tested against this;

describe("flattenArray", () => {
it("returns a flat array with all the elements of the nested arrays in      their original order", () => {
  let arrayOfArrays, expected;
  arrayOfArrays = [[1, 2], [], [3], ["hello", true]];
  expected = [1, 2, 3, "hello", true];
  expect(flattenArray(arrayOfArrays)).to.eql(expected);

  arrayOfArrays = [[1], [2], [[3, 4]]];
  expected = [1, 2, [3, 4]];
  expect(flattenArray(arrayOfArrays)).to.eql(expected);
});
it("does not mutate the passed array, i.e. returns a new array, leaving the original untouched", () => {
  const original = [[1, 2], [], [3, 4]];
  const flat = flattenArray(original);
  expect(original).to.not.equal(flat);
  expect(original).to.eql([[1, 2], [], [3, 4]]);
});
});

I have no clue how else to try and solve this question, does anyone have any sugestions.

2
  • 1
    do you need to flat only one level, or more? btw, you need to return the reduced value in the function. Commented Dec 19, 2018 at 17:26
  • 1
    Although not supported by IE, on good browsers you can use Array.prototype.flat() Commented Dec 19, 2018 at 17:31

3 Answers 3

1

You need to return the reduced array by taking the handed over array arrayOfArrays.

function flattenArray(arrayOfArrays) {  
    return arrayOfArrays.reduce(function(a, b) {
        return a.concat(b);
    }, []);
}

For multiple nested arrays, you need to check for array and use a recursion of the function.

function deepFlattenArray(arrayOfArrays) {
    return arrayOfArrays.reduce(function(a, b) {
        return a.concat(Array.isArray(b) ? deepFlattenArray(b) : b);
    }, []);
}

function flattenArray(arrayOfArrays) {
    return arrayOfArrays.reduce(function(a, b) {
        return a.concat(b);
    }, []);
}

console.log(deepFlattenArray([[[1, 2], [3, [4, 5], 6], 7], 8]));
console.log(flattenArray([[[1, 2], [3, [4, 5], 6], 7], 8]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps not the most elegant solution, but this will flatten any number of nested arrays. It's a recursive function that modifies a new array as a side-effect:

var arrOfArrs = [[1, 2, 3], [4, 5, 6], 7, [8, [9, 10, 11, 12]]];
newArr = [];
function flattenArray(arr) {
    for(var i=0; i < arr.length; i++) {
        typeof arr[i] == 'object' ? flattenArray(arr[i]) : newArr.push(arr[i]);   
    }
}

flattenArray(arrOfArrs);
console.log(newArr);

Comments

1

You can use reduce plus spread operator. Just coded this for the particular question but seems to work ok. It uses recursion and works for Multiple nested arrays.

function flatArray(a){
  return a.reduce( (accumulator, current) => {
    if (!Array.isArray(current)) return [...accumulator, current];
    return flatArray([...accumulator, ...current]);
  }, []);
}

let a = [ 1, [2], [3,4], [], [5], [6, [7]], [[[8]]] ];

console.log(flatArray(a));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.