Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flat method Tip #439

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ There are a lot of ways to get updates, choose your own
> Don't forget to Star★ the repo, as this helps promoting the project!

# Tips list

- 75 - [Using flat method in JS]()
- 74 - [Check the reason make your page re-render by changed props and state](http://www.jstips.co/en/react/trace-the-reason-make-your-page-rerender/)
- 73 - [Hash maps without side effects](http://www.jstips.co/en/javascript/hash-maps-without-side-effects/)
- 72 - [Adventurers Guide to React (Part I)](http://www.jstips.co/en/react/adventurers-guide-to-react/)
Expand Down
31 changes: 31 additions & 0 deletions _posts/en/javascript/2021-02-20-using-flat-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: post

title: What is the flat method?
tip-number: 75
tip-username: katiie
tip-username-profile: https://github.com/katiie
tip-tldr: The flat method is used to flatten an array

categories:
- en
- javascript
---

The Flat operation creates a new array from an array with all sub-array elements concatenated into it recursively up to the specified depth.
This method can be used to flatten the records into one array.

```javascript
// Flattening arrays and objects
let arr1 = [["$6"], ["$12"], ["$25"], [["$18"]]];
let flattenedArray = arr1.flat();
console.log(flattenedArray);
// Output: [ $6, $12, $25, ["$18"] ]

```javascript
flattenedArray = arr1.flat(2);
console.log(flattenedArray);
// Output: [ $6, $12, $25, $18 ]

```javascript
```