Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To filter a specific "collection" from a JSON fetch, you would use the filter() method.

For example, if you have a JSON response that looks like this:

{
   "collections": [
      {
         "name": "Collection 1",
         "category": "Category A"
      },
      {
         "name": "Collection 2",
         "category": "Category B"
      },
      {
         "name": "Collection 3",
         "category": "Category C"
      }
   ]
}

You can filter the "Collection 1" by using the filter() method as follows:

fetch('URL-to-JSON').then(response => response.json()).then(data => {
  const collection1 = data.collections.filter(collection => collection.name === 'Collection 1');
  console.log(collection1);
});

This will return an array with the object that matches the name "Collection 1".