Filter:
Allows you to insert each item from an array into a function that returns a boolean. If shoving the item into the function elicits a return value of "true" , then that item , along with any other truth-returning items from the original array, are inserted into a new array which becomes the return value of the filter.
syntax:var filteredArray = [array of data].filter(PUT BOOLEAN FUNCTION BETWEEN THESE PARENTHESIS);
example:
Holly, a scarlet-haired astrophysicist with a schedule even tighter than her waist line, decided that it was time to stop playing the field and find a man to with whom she could start a family. Keen to keep her gene-pool as gingery fresh as possible, she decided to throw together a quick API to scrape data from hot-n-both-red.com, redI.com and crimsonlovers.com that would return the profiles of only the men that she deems to be desirable mates (ie 6'3 or taller, dog lovers making 65k minimum salary in a science related field). Below is an excerpt of some of the data she scraped:
var scrappedProfiles = [
{name: "Dr Sex Panther", heightInCm: 160 , age: 35, income: 150000, profession: "Doctor", hobbies: "searching for my soul mate", pets: "has cats / likes dogs and cats", motto: "Trust me, I'm a doctor" },
{name: "DJ Swizzle Stick", heightInCm: 175, age: 25, income: 35000, profession: "valet", hobbies: "gangsta rap, football, hunnies", pets: "has cats / likes dogs and cats", motto: "I got 99 problems, but I'd like one more"},
{name: "big D", heightInCm: 26 , age: 69, income: "mypenis", profession: "gigolo", hobbies: "blow, blowin', gettin' blown", pets: "yes I do", motto: "fuck bitches get money" },
{name: "Zenith", heightInCm: 185, age: 25, income: 55000, profession: "Astronomy research", hobbies: "fishing, camping, walking my dogs", pets: "has dogs / likes dogs", motto: "stay away from the dark side"}
];
After looking at the shape of the data, Holly designed the following filter that employed regex to search for certain key words in her prospective suitors profession and pet info as well as an assessment of the numerical value of their height and income.
function dateable(profileObject){
if( profileObject.income >= 65000 && profileObject.heightInCm >= 190 && profileObject.pets.search("dog") != -1 && profileObject.profession.search(/astro|doctor|sci|engineer|research|bio|chem|dev/i) != -1 ){
return true;
}
return false;
}
var results = scrappedProfiles.filter(dateable);
Some:Checks to see if any individual items in an array meets the criteria of a given boolean and then returns true only if at least one item returns true when plugged into the boolean function. Some will return false only if there are no items within the array that meet the condition defined in the boolean function.
syntax:var filteredArray = [array of data].filter(PUT BOOLEAN FUNCTION BETWEEN THESE PARENTHESIS);
example: Frustrated that she was not getting any results back from her dating profile scraper. Holly, decided to broaden her search to include more sites (even some with men with less-favorable hair pigmentation). Not wishing to buy a membership to a site unless it contained some men that met her criteria, Holly modified her original function and ran it on prospective web sites:
function dateable(profileObject){
if( profileObject.income >= 65000 && profileObject.heightInCm >= 190 && profileObject.pets.search("dog") != -1 && profileObject.profession.search(/astro|doctor|sci|engineer|research|bio|chem|dev/i) != -1 ){
return true;
}
return false;
}
var useableSite = scrappedProfiles.some(dateable);
Every:
Checks to see if all individual items in an array meet the criteria of a given boolean and then returns true only if every single item in the array gets a true result from the boolean or returns false if any of the items return false when plugged into the boolean function.
syntax:
var filteredArray = [array of data].every(PUT BOOLEAN FUNCTION BETWEEN THESE PARENTHESIS);
example:Once Holly had a few sites that she knew contained good prospects, she decided to apply one last function before joining a site. She wrote the below to search through the contents of each field on a user's profile and identify any disparaging language towards women.
function respectful(profileObject){
var foulLanguage = [];
Object.keys(profileObject).forEach(function(item, index){
var value = profileObject[item]
if(value.toString().search(/bitch|sukeban|whore|skintern|cunt|bobby soxer/i) != -1){
foulLanguage.push(profileObject[item])
}
})
if (foulLanguage.length === 0 ){
return true;
}
return false;
}
var useableSite = scrappedProfiles.every(respectful);
No comments:
Post a Comment