Skip to content

Instantly share code, notes, and snippets.

@selfup
Created July 28, 2017 06:20
Show Gist options
  • Save selfup/e0738b449b47a52e724622092920c32d to your computer and use it in GitHub Desktop.
Save selfup/e0738b449b47a52e724622092920c32d to your computer and use it in GitHub Desktop.
var names = [
["John", "Brown"],
["Lisa", "May"],
["Henry", "Ford"],
];
function displayLastName(inputName) {
// first we are going to map through the names array
// return null for anything that doesn't match
return names
.map(function(name, index) {
console.log('first map: ', index, name);
if (name[0] === inputName) return name[1];
return null;
})
// now we filter all the nulls out to keep the returned array clean
.filter(function(element) {
console.log('filter: ', element);
return element !== null;
})[0];
// since filter returns an array we just grab the first result
// if there is more than one match, we will still just grab the first result
// this is something to consider as part of your application design
}
var result = displayLastName('John');
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment