question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Case Suggestion: How to Flatten array

See original GitHub issue

Use Array.reduce() to get all elements inside the array and concat() to flatten them.

const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
// flatten([1,[2],3,4]) -> [1,2,3,4]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jadjoubrancommented, Feb 24, 2018
0reactions
jadjoubrancommented, Feb 20, 2018

@kakadiadarpan yeah sure! Please keep it procedural style, similarly to all the use cases on codetogo, for example: How to convert from degree to radian in JavaScript rather than creating a reusable function, simply list the steps that alow you to transform the original input (in our case [1, [2], 3, 4] into a flattened array) Even though people will eventually use it as a function, the aim of codetogo is not to push people to copy paste code, we rather want them to learn, that’s why we list instructions step by step This is also a key differentiation between other awesome repos (such as https://github.com/Chalarangelo/30-seconds-of-code)

Feel free to submit the PR even if you’re not 100% sure that it’s correct, I will help you with comments 😄 thanks for your time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

11 Amazing Examples of JavaScript Flatten Array - eduCBA
Using flatMap(), It maps each value to a new value, and resulting array is flatten to a depty of maximum 1. ... flatMap()...
Read more >
How to flatten nested array in javascript? - Stack Overflow
Conclusion: To flatten an arbitrarily nested array in a functional way we just need a one-liner: const flatten = f => traverse(f) (concat)...
Read more >
How to Flatten a Nested Javascript Array | by Cynthia Okoliezeh
The flat() method has an optional argument known as depth (the number of levels to flatten the array) and returns a new array...
Read more >
Flatten Arrays in Vanilla JavaScript with flat() and flatMap()
An overview of two new methods available on the Array prototype in JavaScript: flat and flatMap.
Read more >
Flatten Array using Array.flat() in JavaScript - Samantha Ming
Here's the syntax for this method: array.flat(<depth>);.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found