Horje
how to flatten array in javascript using foreach loop Code Example
how to flatten array in javascript using foreach loop
function flatten(arr) {
  const result = []

  arr.forEach((i) => {
    if (Array.isArray(i)) {
      result.push(...flatten(i))
    } else {
      result.push(i)
    }
  })
  
  return result
}

// Usage
const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]]

flatten(nested) // [1, 2, 3, 4, 5, 6, 7, 8, 9]




Javascript

Related
Format Mathjax Code Example Format Mathjax Code Example
react store props in state Code Example react store props in state Code Example
message.author Code Example message.author Code Example
flow parsing package.json and showing error Code Example flow parsing package.json and showing error Code Example
js default args Code Example js default args Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10