Created
January 21, 2023 20:05
-
-
Save iamqasimali/7df243b263c2f58030b0b5631cee8e04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment