简体   繁体   中英

convert array from inside of returned object into select item list js

I've looked at several examples here on how to do this and the answer still alludes me.

I have an ajax call that returns an object with a couple properties as well as an array of other objects. I want to take 2 properties from each internal object to create a list. right now, I have code that looks like this:

myMethod: function(data){
    $.each(data, function(){
        $('#mySelectList').append($('<option></option>').text(data.Name).val(data.ID));
    });
}

I have tried JSON.stringify(data.Name) as well, which I believe is needed here, but I think I am accessing the properties incorrectly. The object that is returned looks like this in my chrome dev tools:

Object {BooleanProperty1: true, BooleanProperty2: true, Rows: Array[9]}

and when i drill down into the rows:

0: Object
    ID: 1
    Title: "SomeTitle"
    SomeOtherProperties: propertyData
    //more properties
2: Object
    ID: 2
    Title: "SomeOtherTitle"
    SomeOtherProperties: propertyData
    //more properties
//more objects

How can i access the properties inside this array to use them when creating the list?

data it's the response or the complete array and what you need its each of the elements inside of it, for that you need to specify the index and element parameters in the function and read the values from the element

$.each() documentation

 $.each(data.Rows, function(index, element){
    $('#mySelectList').append($('<option></option>').text(element.Name).val(element.ID));
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM