Useful Array Methods in JavaScript

Yahya Gok
3 min readMay 6, 2020

--

The JavaScript Array object is a global object that is used in the construction of arrays. An array is a special type of variable that allows you to store multiple values in a single variable.[1]

1-) filter()

we have here items array and we want to use filter method to to that array taking only item’s price less than 100. we have a variable called filteredItems.

in items , each item will be item.

filteredItems = items.filter((item) => {

return item.price ,<= 100 })

When we filter the item, new filtered items will be inside of filteredItems. And here on the right side we we do console.log(items) , we could see our all original items and on the buttom right we could see console.log(filteredItems).

2-) map( )

we have here map( ) method. works similarly as filter( ) methods. Here we are taking all the names to the new array called itemNames.We can do the same work for item’s price to write all the prices for every item.

3-) find ( )

for find method we just need to declare a boolean statement as you can see an line 12. find method only return first true element in an array. Here It finds first item’s name book and when we do console.log(foundItem) , It will return book’s object.

4-) some( )

some method works little different than other methods we mentioned earlier.

some ( )methods returns true or false, here It checks every item in an array with price less than 100. If one of them returns true. Method returns true. Not all item has to be true, only one item true would be enough returning the function true.

5-) every

every( ) method is like opposite of some and it checks every item in an array. If one of them is false in an condition, returns false. Every item in an array has to obey given condition to return the function true. Here in the Example item.price < 1000 , our all item are less then 1000. because of this our method returns true.

6-) reduce ( )

reduce ( ) method takes two parameters, in the example currentTotal is starting point, which is 0 here ,that we typed on line 13. in every iteration currentTotal gets up , It collects 0 + previous iteration price.after finishing all iteration , at the end currentTotal becomes one number.

7-) include ( )

this method also , return boolean, if the item has the item that we pass in , it return true or false.

References

[1] , https://www.tutorialrepublic.com/javascript-reference/javascript-array-object.php

https://www.youtube.com/watch?v=R8rmfD9Y5-c

--

--

Yahya Gok
Yahya Gok

No responses yet