Untitled

 avatar
unknown
plain_text
a year ago
334 B
4
Indexable
function flatten(arr) {
  return arr.reduce((acc, res) => {
    if (Array.isArray(res)) {
      acc = acc.concat(flatten(res)); // Corrected function name here
    } else {
      acc.push(res);
    }
    return acc;
  }, []);
}

// Example usage:
console.log(flatten([1, 2, [3, 4], 5, 6, [7, 8]])); // Output: [1, 2, 3, 4, 5, 6, 7, 8]
Editor is loading...
Leave a Comment