1

I have an array of objects and I'd like to filter it based on another array. If filters object value is only one, I can use only filter, but if there are one more values in filters object, nothing show. So use forEach inside the filter and wanted to return fit values. Could you please help me what the issue is? Thanks

Expected result:

[
    {
        "tags": [
            "madewithout__gluten",
            "madewithout__nuts",
        ],
        "metafields": "Side"
    }
]

const data = [
    {
        "tags": [
            "madewithout__gluten",
            "madewithout__nuts",
        ],
        "metafields": "Side"
    },
    {
        "tags": [
            "madewithout__gluten",
        ],
        "metafields": "Side"
    },
    {
        "tags": [
            "madewithout__nuts"
        ],
        "metafields": "Side"
    }
]

const filters = ['gluten', 'nuts'] 

const result = data.filter((v) => {
  filters.forEach((tag) => {
    if(!v.tags.includes(`madewithout__${tag}`))
      return;
  });
});

console.log(result);

2 Answers 2

3

Based on the semantics of the data you provided, you want to check if all filters are contained in tags:

const result = data.filter((v) => 
    filters.every((tag) => v.tags.includes(`madewithout__${tag}`))
)

const data = [
    {
        "tags": [
            "madewithout__gluten",
            "madewithout__nuts",
        ],
        "metafields": "Side"
    },
    {
        "tags": [
            "madewithout__gluten",
        ],
        "metafields": "Side"
    },
    {
        "tags": [
            "madewithout__nuts"
        ],
        "metafields": "Side"
    }
]

const filters = ['gluten', 'nuts'] 

const result = data.filter((v) => 
    filters.every((tag) => v.tags.includes(`madewithout__${tag}`))
)

console.log(result);

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

Comments

2

Instead of .forEach, use .some

const result = data.filter((v) => {
  return !filters.some((tag) => !v.tags.includes(`madewithout__${tag}`))
});

const data = [{
    "tags": [
      "madewithout__gluten",
      "madewithout__nuts",
    ],
    "metafields": "Side"
  },
  {
    "tags": [
      "madewithout__gluten",
    ],
    "metafields": "Side"
  },
  {
    "tags": [
      "madewithout__nuts"
    ],
    "metafields": "Side"
  }
]

const filters = ['gluten', 'nuts']

const result = data.filter((v) => {
  return !filters.some((tag) => !v.tags.includes(`madewithout__${tag}`))
});
console.log(result);


Issues with your code

  1. Array.forEach does not return anything. So your return does not break loop or return value.
  2. Your Array.filter does not have return statement. Due to this, by default everything is falsy (return undefined) and hence empty array

2 Comments

Thanks for your post, but your result is not same as my expected result. I wanted AND result
Yup realised the mistake in condition late. Ideal implementation is in @some-user's answer but do not want to make his answer void, so adding alternate in mine

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.