Skip to content

Instantly share code, notes, and snippets.

View mannuelf's full-sized avatar
👾

Mannuel Ferreira mannuelf

👾
View GitHub Profile
@mannuelf
mannuelf / find.js
Created January 1, 2017 21:08
find helper
var users = [
{ name: 'Alan', surname: 'Shearer' },
{ name: 'Micheal', surname: 'Owen' },
{ name: 'Peter', surname: 'Beardsley' }
];
var user;
/*
for (var i = 0; i < users.length; i++) {
@mannuelf
mannuelf / filter-reject.js
Created January 1, 2017 20:54
Create a function called 'reject'. Reject should work in the opposite way of 'filter' - if a function returns 'true', the item should *not* be included in the new array.
const numbers = [ 10, 20, 30, 40, 50, 60 ];
function reject(numbers, iteratorFunction) {
return numbers.filter(function(number){
return (number < 15);
});
}
@mannuelf
mannuelf / filter-permissions.js
Created January 1, 2017 20:34
Handling Permissions with Filter
const users = [
{ id: 1, admin: true },
{ id: 2, admin: false },
{ id: 3, admin: false },
{ id: 4, admin: false },
{ id: 5, admin: true },
];
let filteredUsers = users.filter(function(user) {
return user.admin === true;
@mannuelf
mannuelf / filter-values.js
Created January 1, 2017 20:32
Filtering Values. Filter the array of numbers using the filter helper, creating a new array that only contains numbers greater than 50. Assign this new array to a variable called 'filteredNumbers'. Don't forget to use the 'return' keyword in the function!
const numbers = [15, 25, 35, 45, 55, 65, 75, 85, 95];
let filteredNumbers = numbers.filter(function(number) {
return number > 50;
});
console.log(filteredNumbers);
@mannuelf
mannuelf / filter-property.js
Created January 1, 2017 20:20
using filter to filter based on specific property
const post = { id: 10, title: 'New Post' };
const comments = [
{ postId: 4, content: 'Great reading' },
{ postId: 3, content: 'Good article' },
{ postId: 10, content: 'Very good reading' }
];
function commentsForPost(post, comments) {
return comments.filter(function(comment) {
@mannuelf
mannuelf / filter.js
Created January 1, 2017 19:36
filtering on an array
const products = [
{ name: 'cucumber', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'celery', type: 'vegatable' },
{ name: 'orange', type: 'fruit' },
];
let filteredProducts = [];
/*
@mannuelf
mannuelf / map-pluck.js
Created January 1, 2017 17:46
Plucking an item out of an array
const cars = [
{ model: 'Ferrari', price: 'R2 500 000' },
{ model: 'Porsche 911', price: 'R1 500 000' },
];
// plucking a value
let prices = cars.map(function(car) {
return car.price;
});
@mannuelf
mannuelf / map.js
Created January 1, 2017 17:41
map helper
var numbers = [1,2,3,4,5,6];
var doubledNumbers = [];
for (var i = 0; i < numbers.length; i++) {
doubledNumbers.push(numbers[i] * 2);
}
var doubled = numbers.map(function(number){
return number * 2;
});
@mannuelf
mannuelf / foreach-area.js
Created January 1, 2017 17:29
Calculate the area of an image using forEach method
var images = [
{ height: 10, width: 30 },
{ height: 20, width: 90 },
{ height: 54, width: 32 }
];
var areas = [];
images.forEach(function(image) {
var calculatedArea = image.height * image.width;
@mannuelf
mannuelf / foreach.js
Created January 1, 2017 14:29
forEach method
// create an array of numbers
var numbers = [1,2,3,4,5];
// create a varaible to hold the sum
var sum = 0;
function adder(number) {
sum += number;
}