Skip to main content
February 17, 2020
Question

Accessing nested objects and arrays for SetItems method

  • February 17, 2020
  • 3 replies
  • 1160 views

Hi,

I have a dropdown menu ("Dropdown1"), that I need to populate with options from the object I have defined below. How would I go about populating Dropdown1 with the names (Name1, Name2, & Name3) from the Class1 array?

var oClassList = 
{
    Class1:
    [
        {name: "Name1", company: "Company1", instructor: "Instructor1"},
        {name: "Name2", company: "Company2", instructor: "Instructor1"},
        {name: "Name3", company: "Company3", instructor: "Instructor1"}
    ],
    Class2:
    [
        {name: "Name1", company: "Company1", instructor: "Instructor2"},
        {name: "Name2", company: "Company2", instructor: "Instructor2"},
        {name: "Name3", company: "Company3", instructor: "Instructor2"}
    ],
    Class3:
    [
        {name: "Name1", company: "Company1", instructor: "Instructor3"},
        {name: "Name2", company: "Company2", instructor: "Instructor3"},
        {name: "Name3", company: "Company3", instructor: "Instructor3"}
    ]
};

 I tried the following code using the setItems method, but I couldn't get it to work:

this.getField("Dropdown1").setItems(oClassList.Class1.name);

 

This topic has been closed for replies.

3 replies

try67
Community Expert
Community Expert
February 17, 2020

You can do it like this:

 

var names = [];
for (var i in oClassList.Class1) names.push(oClassList.Class1[i].name);
this.getField("Dropdown1").setItems(names);

Karl Heinz  Kremer
Community Expert
Community Expert
February 17, 2020

You would have to create a temp array variable that holds the names you want to assign to the dropdown. I would write a function that takes as arguments the class name and the class structure and then extracts the relevant names and returns them in an array variable that you can then use in the setItems() method. There is no direct way go go from the data structure you have to setting the dropdown values. 

Bernd Alheit
Community Expert
Community Expert
February 17, 2020

It must be an array. Read the documentation of setItems.