0

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)

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for this. It returns what I expect. Unfortunately it is an endpoint returning the data in this way.
What does that mean? I did not change the data in any way
sorry it means I did not realise the inner products was not an array and I was trying to dig too deep. You have pointed out the error in my code and helped me resolve the question
"products" is an array, but filter(products << this is one product
yip makes perfect sense when you read it out loud. Thank you again!
2

It looks like you are going one too deep on your filtering:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.