YuLiang
YuLiang

Reputation: 21

how to convert string in array to html element

I want to render some data got from API response and as the code below ,temp array can show but list array can't. this picture link showes the render result

Which I use push() can render sucessfully but which I use for loop to change value in array can't render sucessfully.

I want to know how to fix it, thanks a lot.

let list = JSON.parse(JSON.stringify(responses))
for (let i = 0; i < list.length; i++) {
  for (let j = 0; j < list[i].length; j++) {
    list[i][j] = `<li>${list[i][j]}</li>`
  }
}
let temp = []
for (let i = 0; i < responses.length; i++) {
  temp.push(
    <li className="ssl">
      <div className="box">
        <h3>{responses[i][0]}</h3>
          <ul className="ee">{list[i]}</ul>
      </div>
    </li>
  )
}
setResult(temp)

Upvotes: 0

Views: 538

Answers (1)

YuLiang
YuLiang

Reputation: 21

Just change the following

list[i][j] = `<li>${list[i][j]}</li>`

to

list[i][j] = <li>{list[i][j]}</li>

That's it.

Upvotes: 2

Related Questions