What is the correct fix?
function flatten(arr) {
return arr.reduce((acc, x) =>
Array.isArray(x) ? acc.concat(x) : acc.push(x), []);
}
- Change the initial value to
{}instead - Change
reducetomapin the chain - Remove the
Array.isArray(x)check - Replace
acc.push(x)withacc.concat(x)
push returns a length, not the accumulator; use concat.