I have a json as follows
result= {"cambios":[{"idCard":"5ac8798039a4cff62f780f8e","idList":"5ab7c3aec0fa3a36a67b1c87"},{"idCard":"5ab7c3cc83270bcf19c2774f","idList":"5ab7c3aec0fa3a36a67b1c87"}]}
I want to access idCard and idList, I am trying as follows:
function modificarLista(result){
alert("Estoy en cambiar de lista" + result.cambios);
alert("Estoy en cambiar de lista" + result);
$("#res2").text(result.cambios);
$("#res3").text(result);
$.each(result.cambios, function(i, item) {
$("#res2").text("en el each"+ item.idCard);
alert(item.idCard);
alert(item.idList);
});
};
I think my mistake is that I go directly through the array, and I still need to go through "changes" but I don't know very well what I am missing
At first it can be a bit confusing, but take a look at the example:
As you can see, the first modification I have made is to change the element that you are going to loop through. You will not iterate through 'result' but 'result.changes'. When you do this, the item variable takes that value and you just have to reference idCard or idList.
There are examples in the jquery documentation, this is one of the examples.
As I see it, you have an object, which contains an array-type property and within that you have objects, it would be a question that you insert the first array to be able to access the property that contains the objects. This is an improbisacion, it would be a question that you analyze it and see one more way to solve it. I hope it helps you