The jQuery below illustrates how I get the products from https://dummyjson.com/products. However, I don’t know how to iterate the loop to retrieve each of the 30 products in the link and the details of each product.
$.get("https://dummyjson.com/products/1")
.done(function(data, textStatus, jqXHR){
console.log("typeof(data) is: " + typeof(data));
console.log("data is: "+ data);
console.log(data.length);
var result = [];
result = JSON.parse(JSON.stringify(data));
console.log(typeof(result));
console.log(result.length);
var keys = Object.keys(result);
keys.forEach(function (key) {
console.log("forEach");
console.log(key, result[key]);
//console.log(result[0]);
});
})
.fail(function(jqXHR, textStatus, errorThrown){
alert("error");
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
2
Answers
You should iterate through the object itself instead of the keys. Also, use
let
instead ofvar
(see here).For multiple products you need another for loop to iterate through each product:
Thats is totally depends upon how was your reponse are.
If your response like this.
then you have to use following code:
Otherwise your response something like this.
then you have to use following code: