Created
November 6, 2019 01:59
-
-
Save bradtaniguchi/80095be84f803b71de505811c7217a21 to your computer and use it in GitHub Desktop.
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
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