Reputation:
I have a jQuery function like
Function getCities(abbr) {
$.ajax({
type: "POST",
url: "Main/aaa",
data1: "{}",
success: function (response) {
}
});
As a response I get some object array. Format looks like
[{id: 1, decr: "some string"},
{id: 3, decr: "some string2"}]
etc..(each member of the array has values id and desc)
Problem for me is to parse this response and get some list with id and desc values. I tried to write something like
$.each(response, function (key, value) {
items += "<option value='" + key + "'>" + value + "</option>";
});
but that doesn't work for me.
Is there a problem with each? I checked and I got a response, I just need to find a way to parse it.
Upvotes: 1
Views: 82
Reputation: 8814
Although not your issue, the act of transforming each value in a list to some other value is called in functional languages as mapping. And jQuery does support it:
var options = $.map(response, function(item){
return $('<option />', { value: item.id, text: item.desc })
});
This will return a array of option
elements. Learning how to write in a more functional style when programming in jQuery pays off in the long run.
Upvotes: 0
Reputation: 1821
Your response is JSON type. After reviewing your each() function, I modified your each function as below.
Try this:
$.each(response, function (key, value) {
items += "<option value='" + key + "'>" +value.id+" "+value.decr+ "</option>";
});
Upvotes: 2