Skip to content

Instantly share code, notes, and snippets.

@ronaldgreeff
Last active January 24, 2020 11:04
Show Gist options
  • Save ronaldgreeff/01fa03b8532c18259fef93c187631f2f to your computer and use it in GitHub Desktop.
Save ronaldgreeff/01fa03b8532c18259fef93c187631f2f to your computer and use it in GitHub Desktop.
// for loops are the most performant, but these methods are useful given specific tasks
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 },
]
// filter
// const filteredItems = items.filter((item) => {
// return item.price <= 100
// })
// map
// const itemNames = items.map((item) => {
// return item.name
// })
// find
// const foundItem = items.find((item) => {
// return item.name === 'Book'
// })
// forEach
// items.forEach((item) => {
// console.log(item.name)
// })
// some (any)
// const hasInexpensiveItems = items.some((item) => {
// return item.price <= 100;
// })
// every (all)
// const hasInexpensiveItems = items.every((item) => {
// return item.price <= 100;
// })
// reduce
// inital parameter is what to reduce into + starting value, 0)
// const total = items.reduce((currentTotal, item) => {
// return item.price + currentTotal;
// }, 0)
// includes
// const l = [1,2,3,4,5]
// const includesTwo = l.includes(2)
console.log(includesTwo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment