0

I received data after Fetch request and in this data I get these symbols: ""' and some others.

How to remove them?

Thank you in advance.

Here' the code:

useEffect(() => {
    fetch(
      "https://opentdb.com/api.php?amount=5&category=10&difficulty=easy&type=multiple"
    )
      .then((res) => res.json())
      .then((data) =>
        setMyData(
          //Map data to add an id and boolean isSelected
          data.results.map((question) => ({
            ...question,
            incorrect_answers: [
              ...question.incorrect_answers,
              question.correct_answer
            ].map((answer) => ({
              answer,
              isSelected: false,
              id: nanoid()
            }))
          }))
        )
      );
  }, []);```
Smith82
  • 19
  • 5
  • Does this answer your question? [Unescape HTML entities in JavaScript?](https://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript) – yqlim Jan 24 '23 at 06:46

1 Answers1

0

You can use this regex /&[#A-Za-z0-9]+;/gi

sth like this :

useEffect(() => {
    fetch(
      "https://opentdb.com/api.php?amount=5&category=10&difficulty=easy&type=multiple"
    )
      .then((res) => res.json())
      .then((data) =>
        setMyData(
          //Map data to add an id and boolean isSelected
          data.results.map((question) => ({
            ...question,
            incorrect_answers: [
              ...question.incorrect_answers,
              question.correct_answer
            ].map((answer) => ({
              answer : answer.replace(/&[#A-Za-z0-9]+;/gi,''),
              isSelected: false,
              id: nanoid()
            }))
          }))
        )
      );
  }, []);
Ali Sattarzadeh
  • 3,220
  • 1
  • 6
  • 20