diff --git a/README.md b/README.md index 9a7ed2f4..5717e8f3 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/_posts/en/javascript/2021-02-20-using-flat-method.md b/_posts/en/javascript/2021-02-20-using-flat-method.md new file mode 100644 index 00000000..7f31800e --- /dev/null +++ b/_posts/en/javascript/2021-02-20-using-flat-method.md @@ -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 +```