3

I would like to filter out each in an array if the value of category is of a user selected value.

For example, I have an array of exampleData

[
    {
      "node": {
        "id": "377856cd-9477-58a4-9596-888ebc8c03d5",
        "title": "Tubing & Sledding",
        "category": [
          "Winter"
        ]
      }
    },
    {
      "node": {
        "id": "a7370249-af7a-538a-aaae-91f308575bb8",
        "title": "Snowmobiling",
        "category": [
          "Summer",
          "Winter"
        ]
      }
    },
    {
      "node": {
        "id": "4136389a-77b7-5e07-8c95-1c856983d27b",
        "title": "Snowshoeing",
        "category": [
          "Winter"
        ]
      }
    },
    {
      "node": {
        "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af",
        "title": "X-Country Skiing",
        "category": [
          "Winter"
        ]
      }
    }
  ]

And I would like to filter the array to only return the objects with Summer included in the category field array .

To do this, I have tried

const exampleCategory = exampleData.filter(({ node: category }) => {
  activity.category.includes('Summer');
});

Yet, console.log(exampleCategory) shows with zero results.

What have I done wrong here?

2 Answers 2

1

You should return the boolean result of calling .includes:

const exampleCategory = exampleData.filter(({ node: { category }}) => {
   return category.includes('Summer');
});

Also, you can directly do that without return

const exampleCategory = exampleData.filter(({ node: { category }}) => category.includes('Summer'));

Example

const exampleData = [{    "node": {      "id": "377856cd-9477-58a4-9596-888ebc8c03d5",      "title": "Tubing & Sledding",      "category": [        "Winter"      ]    }  },  {    "node": {      "id": "a7370249-af7a-538a-aaae-91f308575bb8",      "title": "Snowmobiling",      "category": [        "Summer",        "Winter"      ]    }  },  {    "node": {      "id": "4136389a-77b7-5e07-8c95-1c856983d27b",      "title": "Snowshoeing",      "category": [        "Winter"      ]    }  },  {    "node": {      "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af",      "title": "X-Country Skiing",      "category": [        "Winter"      ]    }  }];
const exampleCategory = exampleData.filter(({ node: { category }}) => category.includes('Summer'));

console.log(exampleCategory);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

I feel stupid. Cannot believe that I missed that. Will accept as correct answer as it allows. Cheers
0

You are making two mistakes

  • You are returning undefined from callback passed to filter. You need to explicitly return the result of includes or use Arrow Function to implicitly return the result.

  • You are using includes() on wrong value.activity will be undefined. Simply check for element.node.category.includes

const exampleData = [ { "node": { "id": "377856cd-9477-58a4-9596-888ebc8c03d5", "title": "Tubing & Sledding", "category": [ "Winter" ] } }, { "node": { "id": "a7370249-af7a-538a-aaae-91f308575bb8", "title": "Snowmobiling", "category": [ "Summer", "Winter" ] } }, { "node": { "id": "4136389a-77b7-5e07-8c95-1c856983d27b", "title": "Snowshoeing", "category": [ "Winter" ] } }, { "node": { "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af", "title": "X-Country Skiing", "category": [ "Winter" ] } } ]

const exampleCategory = exampleData.filter(x => x.node.category.includes('Summer'))

console.log(exampleCategory)

You can also use Object Destructuring.

const exampleData = [ { "node": { "id": "377856cd-9477-58a4-9596-888ebc8c03d5", "title": "Tubing & Sledding", "category": [ "Winter" ] } }, { "node": { "id": "a7370249-af7a-538a-aaae-91f308575bb8", "title": "Snowmobiling", "category": [ "Summer", "Winter" ] } }, { "node": { "id": "4136389a-77b7-5e07-8c95-1c856983d27b", "title": "Snowshoeing", "category": [ "Winter" ] } }, { "node": { "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af", "title": "X-Country Skiing", "category": [ "Winter" ] } } ]

const exampleCategory = exampleData.filter(({node:{category}}) => category.includes('Summer'))

console.log(exampleCategory)

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.