Every()
Given data:
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 }
]
Every method
this method returns a boolean value and is great for asking a question of your data and getting simple response in return. where as some
just returns true as soon as it finds a single positive case, every
reviews every value and only returns its result after every value has passed its check.
const hasExpensiveItems = items.every((item) => {
return item.price <= 100
})
Result:
false
for each item in items, iare all items less than or equal to 100? if so return true, otherwise false.