avatarfatfish

Summary

This article discusses six cool array functions in ES6 that every JavaScript developer should know.

Abstract

The article introduces six useful array functions in ES6 that can help developers write cleaner and more efficient code. These functions include Array.of, Array.from, includes, at method, flat, and findIndex. Array.of allows for consistent array initialization, while Array.from converts array-like objects into real arrays. The includes method simplifies conditional statements, and the at method provides an alternative way to access array elements. The flat method flattens nested arrays, and findIndex returns the index of the first element that satisfies a provided testing function.

Opinions

  • The author believes that using these ES6 array functions can help developers write less code and do more.
  • The author suggests that the Array.of method can make up for the shortcomings of the Array function.
  • The author emphasizes that the includes method can simplify conditional statements.
  • The author introduces the at method as a magic tool for reading array elements.
  • The author highlights that the flat method can create a new array with all sub-array elements concatenated into it recursively up to the specified depth.
  • The author explains that the findIndex method returns the index of the first element in the array that satisfies the provided testing function.
  • The author concludes by thanking the reader for their time and encourages them to follow and read more high-quality articles.

6 Cool Array Functions in ES6 You Must Know

6 ES6 Tricks That Make You Write Less And Do More.

1. Array.of

About the weird Array function:

As we all know, we can do the following things through the Array function.

  1. Initialize an array of the specified length
  2. Set the initial value of the array

The number of parameters passed to the Array function is not the same, and its function is not the same. This often confuses me.

Fortunately, we can use Array.of to make up for the shortcomings of Array.

2. Array.from

From the method, we can convert array-like objects, arguments objects, and NodeList objects into real arrays through Array.from the method.

  1. Array-like object

2. NodeList

3. Arguments

4. The second parameter of Array.from

We can use the Array.from method like “[].map”.

3. includes

We often write such judgment statements to do something when one of the conditions is met.

In fact, you can simplify the code through the include method.

4. Use the “at method” to read the tail element of an array

How do you read the tail element of the array? Yes, we need to read with “array.length-1” as a subscript.

Is there any other way?

Yes, the “at” method will be your magic. Of course, you can read elements elsewhere in the array.

5. flat

From MDN: “The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.”

6. findIndex

From MDN: “The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.”

Finally

Thanks for reading. Looking forward to your following and reading more high-quality articles.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.
Front End Development
JavaScript
Programming
React
Vuejs
Recommended from ReadMedium