Last active
November 6, 2017 09:30
-
-
Save alexhawkins/307a82e94a4a3321041d2651dab1912a to your computer and use it in GitHub Desktop.
Array iteration with async/await, Multi-dimensional array flattening, Object generation from Array with es6, remove duplicates from array of large objects
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
import { map } from 'p-iteration'; | |
import _uniqBy from 'lodash/uniqBy'; | |
/* Array iteration with async/await */ | |
const getMultipleCategoryProducts = async productCategories => { | |
return map(productCategories, async category => { | |
const products = await getProductsByCategory(category); | |
return products; | |
}); | |
}; | |
filteredProds = await getMultipleCategoryProducts(productCategories); | |
/* flatten mutli-dimensional Array */ | |
filteredProds = [].concat(...filteredProds); | |
/* generate Object from array for O1 constant lookup */ | |
const excluded = Object.assign( | |
...productsExclude.map(productId => ({ [productId]: true })), | |
); | |
/* filter products using Object above */ | |
filteredProds = filteredProds.filter( | |
product => !excluded[product.productId], | |
); | |
/* make sure products are unique */ | |
filteredProds = _uniqBy(filteredProds, (p) => p.productId); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment