DEV Community

Miguel Ramirez
Miguel Ramirez

Posted on

Javascript [array methods] - some,every, find, filter, map, reducer

const items = [
    { name: "item_1", price: 10.3 },
    { name: "item_2", price: 7 },
    { name: "item_3", price: 1.2 }
]


const some = items.some((i) => i.price > 10);
const every = items.every((i) => i.price > 10)
const find = items.find((i) => i.price > 10);
const filtered = items.filter((i) => i.price > 10);
const mapped = items.map((i)=> "[".concat(i.name,"]"))
const reduced = items.reduce((acc, current) => acc + current.price, 0)

console.debug(`
someItem>10: ${some} 
eachItem>10: ${every} 
found: ${JSON.stringify(find)} 
filtered: ${JSON.stringify(filtered)} 
mapped: ${JSON.stringify(mapped)}
reduced: ${reduced}`
)
Enter fullscreen mode Exit fullscreen mode

result:

[DBG]: "
someItem>10: true
eachItem>10: false
found: {"name":"item_1","price":10.3}
filtered: [{"name":"item_1","price":10.3}]
mapped: ["[item_1]","[item_2]","[item_3]"]
reduced: 18.5"

Top comments (0)