0

I have an array of JSON data I am using. Each array item is a blog category, but in the JSON it is presented as the category id#. With the code below, I'm not sure how to set a new array that essentially converts the category id# to the category name. I would have to set what each id# is supposed to be named.

const ButtonCategories = (productCategories, setCategory) => {
  return (
    productCategories.map(category => (
      <button
      key={category}
      className={`filter-button btn-${category}`}
      onClick={() => setCategory(category)}
    >
      {category}
    </button>
  )));
}
1
  • 1
    Could you share a sample json data Commented Aug 26, 2020 at 13:59

1 Answer 1

1

You could store a category id mapping in a variable as below,

const categoryMapping = {
  id1: "Category 1 Name",
  id2: "Category 2 Name",
  id3: "Category 3 Name"
}

and the array, you can map like this,

const transformedBlogs = blogs.map(blog => {
    return {
      ...blog,
      id: categoryMapping[blog.id]
    };
  });
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.