我正在寻找根据嵌套对象中的值过滤数组的方法。
我的代码如下,以查找包含“ Credit”的“ inventory_type”的产品,但我收到一个错误消息,说其中一些不是函数,并且需要一些指导,以寻求在数组深处进行搜索的最佳方法< / p>
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)
答案 0 :(得分:3)
您的“内部产品”不是数组而是一个产品
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)
答案 1 :(得分:2)
您似乎在过滤方面做得太深了:
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(product => (product.inventory_type === 'Credit'))
})
console.log(newArr)