JavaScript Trying to apply a filter to a nested array full of objects

I have a resources array which is full of objects. Each object has categories array full of objects. I am trying to apply a filter to only return resources that have category objects of a specific name. I am having some trouble with the nesting of my data object.

Here is the data I am working with:

const resources = [
  {
    title: 'Learn JS',
    categories: [
      {
        name: 'javascript'
      },
      {
        name: 'css'
      }
    ]
  },
  {
    title: 'Learn CSS',
    categories: [
      {
        name: 'css'
      }
    ]
  },
  {
    title: 'Learn other stuff',
    categories: [
      {
        name: 'jQuery'
      },
      {
        name: 'javascript'
      }
    ]
  },
  {
    title: 'Learn node',
    categories: [
      {
        name: 'node'
      }
    ]
  },
  {
    title: 'Learn React',
    categories: [
      {
        name: 'react'
      }
    ]
  },

];

Here are my two attempts. Both return empty arrays. Am I wrong to be trying to use maps and filters. Is a for loop necessary?

//GOAL: Return only the resources that have a category with name 'javascript'
const attemptOne = resources.filter((item) => {
  return item.categories.forEach((thing, index) => {
    return thing[index] === 'javascript'
  });
}).map((item) => {
  return item;
})

const attemptTwo = resources.filter((item) => {
  item.categories.filter((ci) => {
    return ci.name === 'javascript'
  }).map((nextItem) => {
    return nextItem;
  });
})

I have been stumbling around with this for a while, and I am not sure if I am just over complicating it our what. Thanks in advance!

Answer:1

You can use filter on resources. Inside the filter, since you already know that an object has categories, you can just use some to check if the category name you are looking for is included

const resources = [{
  title: 'Learn JS',
  categories: [{
    name: 'javascript'
  }, {
    name: 'css'
  }]
}, {
  title: 'Learn CSS',
  categories: [{
    name: 'css'
  }]
}, {
  title: 'Learn other stuff',
  categories: [{
    name: 'jQuery'
  }, {
    name: 'javascript'
  }]
}, {
  title: 'Learn node',
  categories: [{
    name: 'node'
  }]
}, {
  title: 'Learn React',
  categories: [{
    name: 'react'
  }]
}];

function filterViaCategory(arr, category) {
  return arr.filter(obj => obj.categories.some(cat => cat.name === category));
}

console.log(filterViaCategory(resources, 'javascript'));
Answer:2

How do I set up CORS on Amazon S3 to allow only approved domains to access a JS script in my S3 bucket? Currently, I have CORS set as follows on my S3 bucket: <?xml version="1.0" encoding="UTF-8"?&...

How do I set up CORS on Amazon S3 to allow only approved domains to access a JS script in my S3 bucket? Currently, I have CORS set as follows on my S3 bucket: <?xml version="1.0" encoding="UTF-8"?&...

  1. restrict access using permission set
  2. restrict access using xhost
  3. restrict access using htaccess
  4. restrict access using sql
  5. restrict access using iis
  6. restrict network access using firewall-cmd/firewall
  7. restrict internet access using group policy
  8. restrict internet access using active directory
  9. restrict ec2 access using tags
  10. restrict folder access using htaccess
  11. restrict network access using firewall-cmd/firewall rhcsa
  12. restrict internet access using dns
  13. restrict internet access using hosts file
  14. restrict access to websites using group policy
  15. restrict access to web page using web.config

So basically I am using a jquery plug-in called blueprint split layout. The code for it can be found at: http://tympanus.net/Blueprints/SplitLayout/index.html. I modified the code to fit with my ...

So basically I am using a jquery plug-in called blueprint split layout. The code for it can be found at: http://tympanus.net/Blueprints/SplitLayout/index.html. I modified the code to fit with my ...

I would like to use JavaScript to read the entire content of a txt file on Google Drive or Dropbox into a string variable. I've seen similar questions, but I would like to have it done purely with ...

I would like to use JavaScript to read the entire content of a txt file on Google Drive or Dropbox into a string variable. I've seen similar questions, but I would like to have it done purely with ...

  1. javascript read text file
  2. javascript read text file into array
  3. javascript read text file from url
  4. javascript read text file from server
  5. javascript read text file into variable
  6. javascript read text file line by line
  7. javascript read text from webpage
  8. javascript read text from image
  9. javascript read text from pdf
  10. javascript read text file example
  11. javascript read text from clipboard
  12. javascript read text file line by line example
  13. javascript read text input value
  14. javascript read text file from local
  15. javascript read text box
  16. javascript read text file from disk
  17. javascript read text file as string
  18. javascript read text input
  19. javascript read text from textarea
  20. javascript read text file into table

I'm using jointjs to draw graphs. I'm wondering how to listen to the mouse click event on an element? I found on http://www.jointjs.com/api#joint.dia.Element, there is only change:position option but ...

I'm using jointjs to draw graphs. I'm wondering how to listen to the mouse click event on an element? I found on http://www.jointjs.com/api#joint.dia.Element, there is only change:position option but ...