Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamqasimali/388ada25d674b6cd339f1f31593b2e7e to your computer and use it in GitHub Desktop.
Save iamqasimali/388ada25d674b6cd339f1f31593b2e7e to your computer and use it in GitHub Desktop.
var luke = {
name: "luke skywalker",
jedi: true,
parents: {
father: {
jedi: true
},
mother: {
jedi: false
}
}
}
var han = {
name: "han solo",
jedi: false,
parents: {
father: {
jedi: false
},
mother: {
jedi: false
}
}
}
var anakin = {
name: "anakin skywalker",
jedi: true,
parents: {
mother: {
jedi: false
}
}
}
var characters = [luke, han, anakin];
function fatherWasJedi(character) {
var path = "parents.father.jedi";
return path.split(".").reduce(function(obj, field) {
if (obj) {
return obj[field];
}
return false;
}, character);
}
characters.forEach(function(character) {
console.log(character.name + "'s father was a jedi:", fatherWasJedi(character))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment