Array Iteration

ARRAY ITERATOR METHODS.

WHAT IS ARRAY ITERATOR?.... An array is a single variable that is used to store different elements.

Array iteration-This is used to traverse an array and dynamically access elements of that array.

  • it operates on every array item.
  • accessing each element of array one by one.

Examples of iteration methods ….. forEach(),filter(),map(),reduce(),reduceRight,every(),some(),and find().

ForEach()

To -do -list. 1.define forEach 2.how is forEach array used ….its advantages and disadvantages . 3.syntax of forEach(). 4.example of for each arrays in code examples.

DEFINE forEach()

carbon (4).png

Output carbon (5).png

Advantages.

  1. It eliminates the possibility of bugs
  2. makes the code more readable.
  3. It is known as the for-each loop since it traverses each element one by one.

##Disadvantages

  1. It is generic…this is because the call back function we pass to it can contain whatever functionality we like .
  2. It is slower to execute compared to for()

The forEach method requires three parameters, one for the current element’s value, the second parameter for the current element’s index, and the final one is for the array upon which the forEach() method has to work.

SYNTAX

carbon (6).png

##Maps

The map() method creates a new array populated with the results of calling a provides function on every element in the calling array

MAP syntax

carbon (7).png

carbon (8).png

using value and index

carbon (9).png

Using array

carbon (10).png

##find

The find method returns the is element in the provided array that satisfies the provided testing function if no value satisfies the testing function undefined is returneds

Find Example

carbon (11).png

Find Syntax

carbon (12).png

Resources linuxhint.com/array-iteration-methods-javas.. w3schools.com/js/js_array_iteration.asp developer.mozilla.org/en-US/docs/Web/JavaSc..

Happy Coding