7

8 Ways to Iterate over an Array in JavaScript

 2 years ago
source link: https://blog.bitsrc.io/8-ways-to-iterate-over-an-array-in-javascript-6660af94738
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

8 Ways to Iterate over an Array in JavaScript

Do you know all of these?

1*RMVZ8Wcx-juN4H7u8BSchQ.png

An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.

Let’s look into the different ways in which we can iterate over an array:

For Loop

A for loop is a control flow statement for specifying iteration that allows code to be executed repeatedly.

1*yuT4D6qV2CuueeOm-arFaw.png

While Loop

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

1*Nq3pruKIB_2Ta8h-kKfgAw.png

ForEach Loop

The forEach loop is used to iterate over the elements of the collection. The collection may be an array or a list.

1*3GWzws3cH48FS499T2NKsg.png

Every()

The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements.

1*d2lf4eZ0iKNpXLFekeALxg.png

Some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function

1*n2DfGxbJ2HV3oUTKCdHF8w.png

Filter()

The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

1*j5aUU9xMI2CfDpdl1v37QA.png

Map()

Map is a collection of key/value pairs. Map keys and values can be of any type (arrays, objects, numbers, and other primitive data types). Map iterates its elements in insertion order. Before the introduction of map, you would have to use an object.

1*_1ISRmlRZAE7TGGK47kY-A.png

Reduce()

The reduce() method executes a reducer function for array element. The reduce() method returns a single value: the function’s accumulated result. The reduce() method does not execute the function for empty array elements.

1*3Vf4IMDdjODh_LO4qWB1VA.png

Wrapping It Up!

Congratulations on reaching this far! You’re a fantastic reader!!

These are some of the best methods to iterate over an array in javascript. You can dig deeper to find out all the possible ways for iterating an array in JavaScript.

If you wish, you can support me by buying me a Chai.

Follow me here :)

well as on Twitter.

Keep Learning!

Build apps with reusable components like Lego

1*mutURvkHDCCgCzhHe-lC5Q.png

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK