Working with arrays in JavaScript used to be a pain with barely any support for complex array operations. Fast forward to today, though, and there are tons of amazing JavaScript array methods available to us. In this video I will be covering the 8 most important array methods in JavaScript.

  1. [0:20] - filter
  2. [1:58] - map
  3. [2:54] - find
  4. [3:42] - forEach
  5. [4:30] - some
  6. [5:50] - every
  7. [6:27] - reduce
  8. [8:51] - includes

1. Filter Method

Filter Method is simply filtered out the items from an array.

const filteredItems = items.filter((item) => {
    return item.price <= 100
});

2. Map Method

Map method allows to take one array and converted into a new array and put an item of an array into the new array.

convert one array to another 

const itemNames = items.map((item) => {
    return item.name
});

3. Find Method

Find method is allowed to get the first value or first object of the function that satisfies the provided testing condition.

return very first item in array

const foundItem = items.find((item) => {
    return item.name == "Antonio"
});

4. ForEach Method

ForEach is just unlike with other methods does not return anything its work similar to For Loop but it has a parameter. So in this loop, we just print out what we want.

items.forEach((item) => {
    //here do something with item
    console.log(item.name)
});

5. Some Method

It is a bit different from other methods instead of returning the brand new array it returns true or False.

return true if at least one item returns true 
  
number cheapPrice = 100;

boolean hasSomeCheapItems = items.some((item) => {
  return item.price <= cheapPrice     
})

6. Every Method

Every method is same as Some method but instead for a checking one item it checks every single item fall under it.

//return true if every item returns true 

let items = [
    { name: 'Bike',     price: 400 },
    { name: 'TV',       price: 1200 }
];

let cheapPrice = 1000;
let everyItemsHasCheapPrice = items.every((item) => {      
  return item.price <= cheapPrice     
});

everyItemsHasCheapPrice // false

7. Reduce Method

Reduce method bit different from all the methods we see above. Reduce method doing some operation on an array and returning the combination of all those operations.

reduce method do some operations on the array and returns the new array as result:

e.g. get total price of all items:

let initialValue = 0; 

const total = items.reduce((reducerVal, item) => {
  return item.price + reducerVal 
}, initialValue )

8. Include Method

This method is just to check whether the value of an array includes in that array or not.

const items = [ 1 , 2, 3, 4, 5 ]
 
boolean includesTwo = items.includes(2)   // true
boolean includesTwo = items.includes(7)   // false

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

#javascript #web-development

8 JavaScript Array Methods You Should Know
87.90 GEEK