Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active November 6, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/307a82e94a4a3321041d2651dab1912a to your computer and use it in GitHub Desktop.
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
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