Skip to content

Instantly share code, notes, and snippets.

@chris-gaona
Last active March 18, 2017 14:30
Show Gist options
  • Save chris-gaona/79d5ecae16523cc1b98acefaeba6efab to your computer and use it in GitHub Desktop.
Save chris-gaona/79d5ecae16523cc1b98acefaeba6efab to your computer and use it in GitHub Desktop.
Looking at the difference between for loop, forEach, .map, & .filter methods + flattening arrays of arrays
// // FIRST WAY
// // using a simple for loop
// function getStockSymbols(stocks) {
// var results = [],
// counter,
// stock;
//
// for (counter = 0; counter < stocks.length; counter++) {
// stock = stocks[counter];
// results.push(stock.symbol);
// }
//
// return results;
// }
// // SECOND WAY
// // using the es6 forEach iterator
// function getStockSymbols(stocks) {
// // run forEach to iterate over array
// var results = [];
//
// stocks.forEach(function (stock) {
// results.push(stock.symbol);
// });
//
// return results;
// }
// // THIRD WAY
// // using the .map method
// // map creates a new array
// function getStockSymbols(stocks) {
//
// return stocks.map(function (stock) {
// return stock.symbol;
// });
// }
// var symbols = getStockSymbols([
// { symbol: "XFX", price: 240.22, volume: 23432 },
// { symbol: "TNZ", price: 332.19, volume: 234 },
// { symbol: "JXJ", price: 120.22, volume: 5323 },
// ]);
// console.log(JSON.stringify(symbols));
// // FILTER FOR SPECIFIC ITEMS IN ARRAY
// // using .filter method
// // filter creates a new array
// function getStocksOver(stocks, minPrice) {
// return stocks.filter(function(stock) {
// return stock.price >= minPrice;
// });
// }
//
// var expensiveStocks = getStocksOver([
// { symbol: "XFX", price: 240.22, volume: 23432 },
// { symbol: "TNZ", price: 332.19, volume: 234 },
// { symbol: "JXJ", price: 120.22, volume: 5323 },
// ],
// 150.00);
//
// console.log(JSON.stringify(expensiveStocks));
///////
// NEXT SECTION COMBINING THEM ALL TOGETHER
///////
// var stocks = [
// { symbol: "XFX", price: 240.22, volume: 23432 },
// { symbol: "TNZ", price: 332.19, volume: 234 },
// { symbol: "JXJ", price: 120.22, volume: 5323 }
// ];
//
// var filteredStockSymbols =
// stocks
// // filter for only those stocks that are greater than or equal to 150
// // filter returns a new array
// .filter(function (stock) {
// return stock.price >= 150.00;
// })
// // map returns a new array
// .map(function (stock) {
// return stock.symbol;
// });
//
// filteredStockSymbols.forEach(function (symbol) {
// console.log(symbol);
// });
////////////////
// ADDS NEW METHOD TO ARRAY PROTOTYPE TO LOOP THROUGH NESTED ARRAYS AND FLATTEN
////////////////
// var exchanges = [
// [
// { symbol: "XFX", price: 240.22, volume: 23432 },
// { symbol: "TNZ", price: 332.19, volume: 234 }
// ],
// [
// { symbol: "JXJ", price: 120.22, volume: 5323 },
// { symbol: "NYN", price: 88.47, volume: 98275 }
// ]
// ];
//
//
// Array.prototype.concatAll = function() {
// var results = [];
//
// this.forEach(function(subArray) {
// subArray.forEach(function(item) {
// results.push(item);
// });
// });
//
// return results;
// };
//
//
// var stocks = exchanges.concatAll();
//
// stocks.forEach(function(stock) {
// console.log(JSON.stringify(stock));
// });
////////////////
// ADVANCED FLATTENING
////////////////
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;
};
//[1,2,3].map(function(num) { return num + 1; }) -> [2,3,4]
//[1,2].map(function(num) { return [num + 1, num + 2]; }) -> [[2,3],[3,4]]
var christmasEveCloses =
exchanges.
map(function(exchange) {
return exchange.stocks.
map(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
};
});
}).
concatAll();
}).
concatAll();
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