Last active
June 29, 2020 07:46
-
-
Save arafin1/88e69a2ff8ab943698893ef556d1774d to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/haxemip
This file contains hidden or 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 exchanges = [ | |
{ | |
name: "NYSE", | |
stocks: [ | |
{ | |
symbol: "XFX", | |
closes: [ | |
{ date: new Date(2014,11,24), price: 240.10 }, | |
{ date: new Date(2014,11,23), price: 232.08 }, | |
{ date: new Date(2014,11,22), price: 241.09 } | |
] | |
}, | |
{ | |
symbol: "TNZ", | |
closes: [ | |
{ date: new Date(2014,11,24), price: 521.24 }, | |
{ date: new Date(2014,11,23), price: 511.00 }, | |
{ date: new Date(2014,11,22), price: 519.29 } | |
] | |
}, | |
] | |
}, | |
{ | |
name: "TSX", | |
stocks: [ | |
{ | |
symbol: "JXJ", | |
closes: [ | |
{ date: new Date(2014,11,24), price: 423.22 }, | |
{ date: new Date(2014,11,23), price: 424.84 }, | |
{ date: new Date(2014,11,22), price: 419.72 } | |
] | |
}, | |
{ | |
symbol: "NYN", | |
closes: [ | |
{ date: new Date(2014,11,24), price: 16.82 }, | |
{ date: new Date(2014,11,23), price: 16.12 }, | |
{ date: new Date(2014,11,22), price: 15.77 } | |
] | |
}, | |
] | |
} | |
]; | |
Array.prototype.concatAll = function() { | |
var results = []; | |
this.forEach(function(subArray) { | |
subArray.forEach(function(item) { | |
results.push(item); | |
}); | |
}); | |
return results; | |
}; | |
Array.prototype.concatMap = function(projection) { | |
return this. | |
map(projection). | |
concatAll(); | |
}; | |
//[1,2,3].map(function(num) { return num + 1; }) -> [2,3,4] | |
//[1,2].concatMap(function(num) { return [num + 1, num + 2]; }) -> [2,3,3,4] | |
var christmasEveCloses = | |
exchanges. | |
concatMap(function(exchange) { | |
return exchange.stocks. | |
concatMap(function(stock) { | |
return stock.closes. | |
filter(function(close) { | |
return close.date.getMonth() === 11 && | |
close.date.getDate() === 24; | |
}). | |
map(function(close) { | |
return { | |
symbol: stock.symbol, | |
price: close.price | |
}; | |
}); | |
}); | |
}); | |
christmasEveCloses.forEach(function(christmasEveClose) { | |
console.log(christmasEveClose); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Map Examples on multiple nested arrays