jquery – How can I go through an array of json and get its values ​​to show them in the view?


My method to access a json url is as follows and it works, but now I want to go through the json with a for and its ´.length´ but I couldn't.

The json from which I want to get the url is the following


<div id="success">
<div id="artisturl"></div>

   type : 'GET',
   url : 'https://api.spotify.com/v1/search',
   data : {'q':'coldplay',
   dataType : 'json',
   success : function(data) {
      $('#success #artisturl').html(data.artists.items[0].uri) ;
}) ;


And why use jQuery when you can use native JavaScript?
Here's a solution that doesn't require external libraries:

for(let i = 0; i < data.artists.items.length; i++) {
    console.log(data.artists.items[i].href);  // (o el campo que necesites)

I know that you are already using jQuery, but I am in favor of using native tools whenever possible (although I understand that it goes according to taste). Personally, I don't find it more complex than the jQuery solution, and you are more aware of your code.

By the way, you don't need to parse the JSON, since data is already an object.

Scroll to Top