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

Question:

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

http://www.jsoneditoronline.org/?id=74fa8799c027f3af0b2faf44ac1c9e47

<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js</scr>
<div id="success">
<div id="artisturl"></div>
</div>

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

Answer:

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