This file contains 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 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} |
This file contains 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 filterAgeEx1 = users.filter(function(user) { | |
return user.age > 50 | |
}) |
This file contains 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 filterAgeEx2 = users.filter(user => user.age > 50) |
This file contains 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
function hasThanMore50(user) { | |
return user.age > 50 | |
} | |
const filterAgeEx3 = users.filter(hasThanMore50) |
This file contains 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
function hasThanMore(user, age) { | |
return user.age > age | |
} | |
const filterAgeEx4 = users.filter(user => hasThanMore(user, 50)) |
This file contains 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
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) |
This file contains 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
function filterUserHasThanMore50(array) { | |
const passed = [] | |
for (var i = 0; i < array.length; i++) { | |
if(array[i].age > 50) | |
passed.push(array[i]) | |
} | |
return passed | |
} |
This file contains 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 numbers = [3, 1, 6] | |
function sumNumbers(numbers) { | |
let result = 0 | |
for(let i = 0; i < numbers.length; i++) { | |
result += numbers[i] | |
} | |
return result | |
} |
This file contains 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 numbers = [3, 1, 6] | |
const sum = numbers.reduce((sum, item) => sum + item) | |
console.log(sum) // 10 |
This file contains 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 sumSales = (previusSales, nextSales) => previusSales + nextSales | |
const bySales = (item) => item.sales | |
const totalSales = users.map(bySales).reduce(sumSales) | |
console.log(totalSales) // 49991 |
OlderNewer