2

How to compare these arrays? I want to compare and get a result like the below.

Array of string

 ["Typo", "Buttons"]

Array of object

[
  {icon: "General", categoryName: "Buttons"}
  {icon: "DataDisplay", categoryName: "Typo"}
  {icon: "Other", categoryName: "Sliders"}
]

As you can see there is no Sliders categoryName in the array of string. I expected the result should be another array of objects. As the following

[
  {icon: "General", categoryName: "Buttons"}
  {icon: "DataDisplay", categoryName: "Typo"}
]

Thanks!

1 Answer 1

5

You can use .filter as follows:

const categories = ["Typo", "Buttons"]
const items = [
  {icon: "General", categoryName: "Buttons"},
  {icon: "DataDisplay", categoryName: "Typo"},
  {icon: "Other", categoryName: "Sliders"}
]

const res = items.filter(item => categories.includes(item.categoryName));

console.log(res);

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

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.