frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

18 - flatten

Open nitish8899 opened this issue 1 year ago • 0 comments

index.js

export const flatten = function(arr, depth = 1, result = []) {
  for (let i = 0, length = arr.length; i < length; i++) {
    const value = arr[i];
    if (Array.isArray(value) && depth > 0) {
      flatten(value, depth - 1, result);
    } else {
      result.push(value);
    }
  }
  return result;
};

nitish8899 avatar Mar 01 '24 04:03 nitish8899