JS Iterator objects

When we want to iterate through an object we usually fall in the trap (or not) to use always the same iterators, like for or .map(), but there is a whole world of iterators in JS each one with their respective functions and differences between each oth…


This content originally appeared on DEV Community and was authored by Diego Cachafeiro

When we want to iterate through an object we usually fall in the trap (or not) to use always the same iterators, like for or .map(), but there is a whole world of iterators in JS each one with their respective functions and differences between each other.

In this post I will explain to you practical uses and differences between .map(),.filter(), .find(), .reduce() and .forEach().

1. Starting with .map()

The .map() iterator will go through the elements in the array to populate a new array with the results of the function you provide inside of it.

const array = ["name1", "name2", "name3"];

// pass a function to map
const map = array.map((x, i) => {
  return x.concat(` surname${i+1}`);
});

console.log(map);
// expected output: Array ["name1 surname1", "name2 surname2", "name3 surname3"]

As you can see the .map() iterator creates a new array populated with what we passed inside of it.

2. .filter()

The .filter() iterates through the array and will return a new array populated with the elements that passes the condition provided inside of it.

const array = ["name1", "name2", "name3"];

// pass a function to filter
const filter = array.filter(x => !x.includes(2));

console.log(map);
// expected output: Array ["name1", "name3"]

3. .find()

The .find() method will return the first element that passes the condition inside of the array.

const array = ["name1", "name2", "name3"];

// pass a function to filter
const find = array.find(x => x.includes(2));

console.log(map);
// expected output: Array "name2"

4. .reduce()

The .reduce() method will execute a reducer function that you will provide inside of it, this will result in a single output value from the multiple elements inside of the array.

The reducer function can take four arguments:

  1. Accumulator
  2. Current Value
  3. Current Index
  4. Source Array
const reducer = (sum, val) => sum + val;
const initialValue = 0;
const arr = [1, 3, 5, 7];

const sumReduce = arr.reduce(reducer, initialValue);
console.log(sumReduce);
// 16

5. .forEach()

The .forEach() method will execute a function for each one of the elements in the array that iterates.

In case you are wondering why ever use foreach instead of the other ones, well forEach is more of a generic tool provided by Js, I'll tell you to use it when you don't have any better and more specific tool as we saw in the previous methods.

For example, the one that you can confuse it more is with .map() the difference between them is that map is for modifying the array and returning modified, while .forEach() can iterate through the array with any necessary modification like just console.log the array.

tenor

Well I hope this article helped you and provided you with a little more knowledge for your day to day coding, any suggestions or comments are welcome in the comment section.


This content originally appeared on DEV Community and was authored by Diego Cachafeiro


Print Share Comment Cite Upload Translate Updates
APA

Diego Cachafeiro | Sciencx (2021-05-21T08:08:00+00:00) JS Iterator objects. Retrieved from https://www.scien.cx/2021/05/21/js-iterator-objects/

MLA
" » JS Iterator objects." Diego Cachafeiro | Sciencx - Friday May 21, 2021, https://www.scien.cx/2021/05/21/js-iterator-objects/
HARVARD
Diego Cachafeiro | Sciencx Friday May 21, 2021 » JS Iterator objects., viewed ,<https://www.scien.cx/2021/05/21/js-iterator-objects/>
VANCOUVER
Diego Cachafeiro | Sciencx - » JS Iterator objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/21/js-iterator-objects/
CHICAGO
" » JS Iterator objects." Diego Cachafeiro | Sciencx - Accessed . https://www.scien.cx/2021/05/21/js-iterator-objects/
IEEE
" » JS Iterator objects." Diego Cachafeiro | Sciencx [Online]. Available: https://www.scien.cx/2021/05/21/js-iterator-objects/. [Accessed: ]
rf:citation
» JS Iterator objects | Diego Cachafeiro | Sciencx | https://www.scien.cx/2021/05/21/js-iterator-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.