.DS_Store
# reduce-data-with-javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Plunker</title>
</head>
<body>
<script>
// Allows Plunker to display logs in preview panel
// for better in-browser experience
var originalConsoleLog = console.log
console.log = function () {
originalConsoleLog.apply(console, arguments)
var args = Array.prototype.slice.call(arguments);
document.body.innerText += args.join(' ') + '\n';
document.body.style['fontFamily'] = 'monospace';
document.body.style['fontSize'] = '1.5em';
};
</script>
<script src="script.js"></script>
</body>
</html>
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))
});