简体   繁体   English

将数组从返回的对象内部转换为选择项目列表js

[英]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. 我有一个ajax调用,该调用返回一个具有几个属性以及其他对象数组的对象。 I want to take 2 properties from each internal object to create a list. 我想从每个内部对象中获取2个属性以创建一个列表。 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. 我也尝试过JSON.stringify(data.Name) ,我认为这是必需的,但是我认为我访问的属性不正确。 The object that is returned looks like this in my chrome dev tools: 在我的chrome开发工具中,返回的对象如下所示:

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 data是响应还是完整的数组,以及其中需要的每个元素,为此,您需要在函数中指定索引和element参数,并从element读取值

$.each() documentation $ .each()文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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