Last active
October 9, 2019 14:15
-
-
Save AvgustPol/2907b73b4eb8e9d48fbbb8b22a7992a1 to your computer and use it in GitHub Desktop.
8 Must Know JavaScript Array Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const filteredItems = items.filter((item) => { | |
return item.price <= 100 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return very first item in array | |
const foundItem = items.find((item) => { | |
return item.name == "Antonio" | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
items.forEach((item) => { | |
//here do something with item | |
console.log(item.name) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = [ 1 , 2, 3, 4, 5 ] | |
boolean includesTwo = items.includes(2) // true | |
boolean includesTwo = items.includes(7) // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = [ | |
{ name: 'Bike', price: 100 }, | |
{ name: 'TV', price: 200 }, | |
{ name: 'Album', price: 10 }, | |
{ name: 'Book', price: 5 }, | |
{ name: 'Phone', price: 500 }, | |
{ name: 'Computer', price: 1000 }, | |
{ name: 'Keyboard', price: 25 }, | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
convert one array to another | |
const itemNames = items.map((item) => { | |
return item.name | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return true if at least one item returns true | |
number cheapPrice = 100; | |
boolean hasSomeCheapItems = items.some((item) => { | |
return item.price <= cheapPrice | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://youtu.be/R8rmfD9Y5-c