You can do something like
for(var k in result) {
console.log(k, result[k]);
}
which loops over all the keys in the returned json and prints the values. However, if you have a nested structure, you will need to use
typeof result[k] === "object"
to determine if you have to loop over the nested objects. Most APIs I have used, the developers know the structure of what is being returned, so this is unnecessary. However, I suppose it's possible that this expectation is not good for all cases.
Answer from hvgotcodes on Stack OverflowYou can do something like
for(var k in result) {
console.log(k, result[k]);
}
which loops over all the keys in the returned json and prints the values. However, if you have a nested structure, you will need to use
typeof result[k] === "object"
to determine if you have to loop over the nested objects. Most APIs I have used, the developers know the structure of what is being returned, so this is unnecessary. However, I suppose it's possible that this expectation is not good for all cases.
Try this:
$.each(result,function(index, value){
console.log('My array has at position ' + index + ', this value: ' + value);
});
Hi all,
Im really struggling with this problem today. So basically, I have an array in the format
arr = [{title: " some title", id: "some id"}, {title: " some title2", id: "some id2"}] and all im trying to do is loop through each item in the array and get the value of the ids.
Here is what ive tried:
for( var i = 0; i< arr.length; i++){
console.log(arr[i].id)
}
It keeps showing up as undefined, please can anyone assist me? I would like the result to be "some id"
Iterate through json with javascript (forEach)
javascript - How do I iterate over a JSON structure? - Stack Overflow
JavaScript Json Array foreach? - Stack Overflow
Loop through JSON array
Videos
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
note: the for-in method is cool for simple objects. Not very smart to use with DOM object.
Taken from jQuery docs:
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});
Your data should already be a javascript array because you've specified the JSON type for the jQuery Ajax call so it should have already parsed the JSON into javascript. As such, you can just directly iterate it as the array:
success: function (data) {
for (var i = 0; i < data.length; i++) {
var checkBox = "<input type='checkbox' data-price='" + data[i].Price + "' name='" + data[i].Name + "' value='" + data[i].ID + "'/>" + data[i].Name + "<br/>";
$(checkBox).appendTo('#modifiersDiv');
}
$('#addModifiers').modal('show');
}
Or, if you want to use jQuery's .each() iterator instead of a for loop, you can do this:
success: function (data) {
$.each(data, function(key, item) {
var checkBox = "<input type='checkbox' data-price='" + item.Price + "' name='" + item.Name + "' value='" + item.ID + "'/>" + item.Name + "<br/>";
$(checkBox).appendTo('#modifiersDiv');
});
$('#addModifiers').modal('show');
}
You shouldn't be using var objects = JSON.stringify(data); since the data is already a JSON object.
Use JSON.stringify to create a string from a object
Use JSON.parse is to create an object from a string
Example:
var data = [{id: 1, name:'personsName'}, {id: 2, name:'personsName2'}]
var string = JSON.stringify(data)
var json = JSON.parse(string)
You can loop trough the data and append by using:
data.forEach(function(key, index){
$("#modifiersDiv")
.append($("<input></input>")
.attr("type", "checkbox")
.attr("data-price",key.Price )
.attr("name",key.Name )
.attr("value",key.ID)
.text(key.Name);
}