0

So I'm making a fetch api with jquery. Everything is normal, but when I loop inside the loop it displays [object object], and when I log into the nested loop the data is normal. this my json

[
  {
    "Title": "Bla Bla",    
    "Author": "Feng Qi Yue",
    "Artist": "Feng Qi Yue",
    "User": {
      "ID": 1,
      "CreatedAt": "2021-02-23T11:16:10.722+07:00",
      "UpdatedAt": "0001-01-01T00:00:00Z",
      "Username": "soel30",
      "Password": "blablbalbala",
      "UserRefer": 1
    },
     "Genre": [
        {
        "ID": 1,
        "CreatedAt": "2021-02-23T09:42:38.681+07:00",
        "UpdatedAt": "2021-02-23T09:42:38.681+07:00",
        "DeletedAt": null,
        "Name": "Adventure",
        "Slug": "adventure"
      },
  }
]

and this my jquery

$(document).ready(function () {
            var url = "http://someurl"
            fetch(url)
                .then(res => res.json())
                .then(data => {
                    $.each(data, function(i, k) {
                        $("tbody.list").append(`
                        <tr>
                            <td>`+ data[i].Title + `</td>
                            <td>`+ data[i].User.Username +`</td>
                            <td>` + data[i].Author + `</td>
                            <td>`+  data[i].Artist +`</td>
                            <td>`+  $.each(data[i].Genre, function(o, l) { data[i].Genre[o].Name })
                                 +`</td> //the problem is here
                            <td>`+  data[i].Release +`</td>
                            </tr>
                        `)
                    })
                })
        }
1
  • @Barmar I fixed it Commented Feb 25, 2021 at 0:53

1 Answer 1

1

Map the Genre array to an array of <td>s with the names, then join by a string, you probably want commas:

<td>`+  data[i].Artist +`</td>
<td>`+ data[i].Genre.map(item => item.name).join(',') + `</td>
<td>`+  data[i].Release +`</td>

I'd also suggest using the ${} syntax provided by template literals to make the syntax cleaner, as well as using the second argument to .each's callback - or, even shorter, invoke the array iterator:

// $.each(data, function(i, song) {
for (const song of data) {
    $("tbody.list").append(`
    <tr>
        <td>${song.Title + `</td>
        <td>${song.User.Username}</td>
        <td>${song.Author}</td>
        <td>${song.Artist}</td>
        <td>${song.Genre.map(item => item.name).join(', ')}</td>
        <td>${song.Release}</td>
    </tr>
    `);
}

Another thing to keep in mind is that direct concatenation of HTML is a security risk when the values being concatenated isn't trustworthy. So make sure the data is good first, or run it through an XSS cleaner.

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

2 Comments

but how to take it into words. because when I put it in the a href, it joined even though I wanted each word to have one a href. Can you explain?
If you want an <a>, you'll have to put that in the HTML string returned by the .map callback. item => `<a href=something>${item.name}</a> or something of the sort

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.