Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created July 10, 2017 20:35
Show Gist options
  • Save prof3ssorSt3v3/c5ef2c44eac2696fe3a6f2a42487786e to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/c5ef2c44eac2696fe3a6f2a42487786e to your computer and use it in GitHub Desktop.
//Arrays of Objects
// Efficiently combining and chaining Array methods and Arrow functions
// person.email.indexOf("@replicant.io") > -1
//Find the names of all the people who have "replicant.io" emails
let people = [
{"id":123, "name":"Rick Deckard", "email":"rick@bladerunner.org"},
{"id":456, "name":"Roy Batty", "email":"roy@replicant.io"},
{"id":789, "name":"J.F. Sebastian", "email":"j.f@tyler.com"},
{"id":258, "name":"Pris", "email":"pris@replicant.io"}
];
//Two step version
let replicants = people.filter(function(person){
return person.email.indexOf("@replicant.io") > -1;
});
let names = replicants.map(function(person){
return person.name;
});
console.log("List of replicant names:", names);
//chain the two steps
let replicantNames = people.filter(function(person){
return person.email.indexOf("@replicant.io") > -1;
}).map(function(person){
return person.name;
});
console.log("List of replicant names:", replicantNames);
//arrow function version
let rn = people.filter( person => (person.email.indexOf("@replicant.io")>-1) )
.map(person => person.name );
console.log("List of replicant names:", rn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment