如何从此对象中检索Constituency_Name

时间:2017-08-25 20:03:07

标签: javascript jquery json

如何从此对象中检索Constituency_Name

[{"Constituency_ID":148,"Constituency_Name":"NA-160"},
{"Constituency_ID":151,"Constituency_Name":"NA-163"},
{"Constituency_ID":149,"Constituency_Name":"NA-161"},
{"Constituency_ID":150,"Constituency_Name":"NA-162"}]
data: { Constituency_ID: Constituency_Name }, success: function (cities)
          $('#Constituency_ID').html("");
$.each(cities, function (key, value) {

                var a = cities.length;
               var we= value[0].map(function (element, index) {
                    return element.Constituency_Name;
                })
                ///parsedData
                var select = document.getElementById('#Constituency_ID');

               // var pos =value[0].Constituency_Name.indexOf("NA");
                option = document.createElement('option'); 
                option.text = we;
                option.value = value[0].Constituency_ID;

                $('#Constituency_ID').append(option);

            });



        }
    });
}

2 个答案:

答案 0 :(得分:1)

    var p = [{"Constituency_ID":148,"Constituency_Name":"NA-160"},{"Constituency_ID":151,"Constituency_Name":"NA-163"},{"Constituency_ID":149,"Constituency_Name":"NA-161"},{"Constituency_ID":150,"Constituency_Name":"NA-162"}]

    var countryNameArray = p.map(function(element,index){
    return element.Constituency_Name;
    })
console.log(countryNameArray[0]) // prints NA-160

答案 1 :(得分:0)

这应该可以解决您的问题

 cities.forEach(function(element,index{
$('#Constituency_ID').append('<option>'+element.Constituency_Name+ '</option>')
        }) 

如果您想为您的选项增加价值,请使用此

cities.forEach(function(element,index{
    $('#Constituency_ID').append('<option value='+element.Constituency_ID+'>'+element.Constituency_Name+ '</option>')
            })