I am looking to filter an array based on a value in a nested object.
My code goes as follows to find the products containing the "inventory_type" of "Credit" but I get an error saying some is not a function and would need some guidance as to the best approach to search this deep in an array
let object = [{
"summary": [{
"count": 2,
"inventory_region": "England & Wales",
"inventory_type": "Add-on"
}, {
"count": 2,
"inventory_region": "England & Wales",
"inventory_type": "Credit"
}, {
"count": 2,
"inventory_region": "England & Wales",
"inventory_type": "Social-boost"
}, {
"count": 2,
"inventory_region": "Scotland",
"inventory_type": "Add-on"
}, {
"count": 2,
"inventory_region": "Scotland",
"inventory_type": "Credit"
}],
"products": [{
"count": 2,
"display_label_short": "14 Day Add On E&W",
"inventory_region": "England & Wales",
"display_label": "14 Day Add On E&W",
"inventory_type": "Add-on"
}, {
"count": 2,
"display_label_short": "Standard 28 Day Listing E&W",
"inventory_region": "England & Wales",
"display_label": "Standard 28 Day Listing E&W",
"inventory_type": "Credit"
}, {
"count": 2,
"display_label_short": "14 Day Boost E&W",
"inventory_region": "England & Wales",
"display_label": "14 Day Boost E&W",
"inventory_type": "Social-boost"
}, {
"count": 2,
"display_label_short": "14 Day Add On SCO",
"inventory_region": "Scotland",
"display_label": "14 Day Add On SCO",
"inventory_type": "Add-on"
}, {
"count": 2,
"display_label_short": "Standard 28 Day Listing SCO",
"inventory_region": "Scotland",
"display_label": "Standard 28 Day Listing SCO",
"inventory_type": "Credit"
}],
"company_id": 2876909,
"company_name": "Automated Testing"
}]
let newArr = object.map(objects => {
return objects.products.filter(products => {
products.some(product => product.inventory_type === 'Credit')
})
})
console.log(newArr)
Your "inner products" is not an array but just one product
let object = [{ "summary": [{ "count": 2, "inventory_region": "England & Wales", "inventory_type": "Add-on" }, { "count": 2, "inventory_region": "England & Wales", "inventory_type": "Credit" }, { "count": 2, "inventory_region": "England & Wales", "inventory_type": "Social-boost" }, { "count": 2, "inventory_region": "Scotland", "inventory_type": "Add-on" }, { "count": 2, "inventory_region": "Scotland", "inventory_type": "Credit" }], "products": [{ "count": 2, "display_label_short": "14 Day Add On E&W", "inventory_region": "England & Wales", "display_label": "14 Day Add On E&W", "inventory_type": "Add-on" }, { "count": 2, "display_label_short": "Standard 28 Day Listing E&W", "inventory_region": "England & Wales", "display_label": "Standard 28 Day Listing E&W", "inventory_type": "Credit" }, { "count": 2, "display_label_short": "14 Day Boost E&W", "inventory_region": "England & Wales", "display_label": "14 Day Boost E&W", "inventory_type": "Social-boost" }, { "count": 2, "display_label_short": "14 Day Add On SCO", "inventory_region": "Scotland", "display_label": "14 Day Add On SCO", "inventory_type": "Add-on" }, { "count": 2, "display_label_short": "Standard 28 Day Listing SCO", "inventory_region": "Scotland", "display_label": "Standard 28 Day Listing SCO", "inventory_type": "Credit" }], "company_id": 2876909, "company_name": "Automated Testing" }]
let newArr = object.map(objects => objects.products.filter(product =>
product.inventory_type === 'Credit'
))
console.log(newArr)