Skip to content

Instantly share code, notes, and snippets.

@bradtaniguchi
Created November 6, 2019 01:59
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 bradtaniguchi/80095be84f803b71de505811c7217a21 to your computer and use it in GitHub Desktop.
Save bradtaniguchi/80095be84f803b71de505811c7217a21 to your computer and use it in GitHub Desktop.
const PACKAGEPRODUCTS = [
{ packagename: "Package 1" },
{
productlist: [
{ productname: "product1" },
{ productname: "product2" },
{ productname: "product3" }
]
},
{ packagename: "Package 2" },
{ productlist: [{ productname: "product3" }, { productname: "product6" }] },
{ packagename: "Package 3" },
{ productlist: [{ productname: "product6" }, { productname: "product9" }] },
{ packagename: "Package 4" },
{
productlist: [
{ productname: "product2" },
{ productname: "product3" },
{ productname: "product5" }
]
}
];
console.log(PACKAGEPRODUCTS.reduce((productNames, productObj) => {
if (productObj.productlist) {
// if the item in the array has the productlist attribute, we
// add it to the array
productNames.push(...productObj.productlist);
}
return productNames;
}, []));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment