Skip to content

Instantly share code, notes, and snippets.

View isacjunior's full-sized avatar

Isac isacjunior

View GitHub Profile
@isacjunior
isacjunior / arrayUsers.js
Last active August 29, 2017 21:07
Usuários
const users =
[
{name: {first: 'Bruce', last: 'Wayne'}, age: 26, sales: 2314},
{name: {first: 'Alvaro', last: 'Angelos'}, age: 55, sales: 1668},
{name: {first: 'Denese', last: 'Dossett'}, age: 29, sales: 9248},
{name: {first: 'Douglas', last: 'Denney'}, age: 49, sales: 5058},
{name: {first: 'Earline', last: 'Erickson'}, age: 19, sales: 18876},
{name: {first: 'Herman', last: 'Hazell'}, age: 25, sales: 2746},
{name: {first: 'Homer', last: 'Hirth'}, age: 26, sales: 474},
{name: {first: 'Hwa', last: 'Heidt'}, age: 32, sales: 9607}
const filterAgeEx1 = users.filter(function(user) {
return user.age > 50
})
const filterAgeEx2 = users.filter(user => user.age > 50)
function hasThanMore50(user) {
return user.age > 50
}
const filterAgeEx3 = users.filter(hasThanMore50)
function hasThanMore(user, age) {
return user.age > age
}
const filterAgeEx4 = users.filter(user => hasThanMore(user, 50))
function filter(array, test) {
const passed = []
for (var i = 0; i < array.length; i++) {
if(test(array[i]))
passed.push(array[i])
}
return passed
}
const filterAgeEx5 = filter(users, user => user.age > 50)
function filterUserHasThanMore50(array) {
const passed = []
for (var i = 0; i < array.length; i++) {
if(array[i].age > 50)
passed.push(array[i])
}
return passed
}
const numbers = [3, 1, 6]
function sumNumbers(numbers) {
let result = 0
for(let i = 0; i < numbers.length; i++) {
result += numbers[i]
}
return result
}
const numbers = [3, 1, 6]
const sum = numbers.reduce((sum, item) => sum + item)
console.log(sum) // 10
const sumSales = (previusSales, nextSales) => previusSales + nextSales
const bySales = (item) => item.sales
const totalSales = users.map(bySales).reduce(sumSales)
console.log(totalSales) // 49991