.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 data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var flattenedData = data.reduce(function(acc, value) {
return acc.concat(value);
}, []);
var input = [
{
title: "Batman Begins",
year: 2005,
cast: [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy"
]
},
{
title: "The Dark Knight",
year: 2008,
cast: [
"Christian Bale",
"Heath Ledger",
"Aaron Eckhart",
"Michael Caine",
"Maggie Gyllenhal",
"Gary Oldman",
"Morgan Freeman"
]
},
{
title: "The Dark Knight Rises",
year: 2012,
cast: [
"Christian Bale",
"Gary Oldman",
"Tom Hardy",
"Joseph Gordon-Levitt",
"Anne Hathaway",
"Marion Cotillard",
"Morgan Freeman",
"Michael Caine"
]
}
];
var stars = input.reduce(function(acc, value) {
value.cast.forEach(function(star) {
if (acc.indexOf(star) === -1) {
acc.push(star);
}
});
return acc;
}, []);
var data = [1, 2, 3, 4, "5"];
var sum = data.reduceRight(function(acc, value, index) {
console.log(`Index: ${index}`)
return acc + value;
}, 0);
console.log("Sum: ", sum)