Get the number of elements in a JSON object
-
This is a very basic question but I'm asking because I really can't find it.
How can we find the length of an object containing a string and color pair data like the one below?
NOTE: Of course,
Select.length
doesn't work here.const Select = { "Alien": 0xFF651313, "Cow": 0xFF291bdf, "Bird": 0xFF20df1b, "Shark": 0xFF77650e };
-
@JulesV You need to run a loop over the object.
reg count = 0; for (x in Select) count++; Console.print(count);
-
@d-healey You've saved me, Thank you!
Can I ask you one last thing?
If I use this in a 2D array format like below, How can I create the ComboBox list with strings that are the first value of each array
[0]
?const Select = [ ["Alien", 0xFF651313], ["Cow", 0xFF291bdf], ["Bird", 0xFF20df1b], ["Shark", 0xFF77650e] ]; const var ComboBox1 = Content.getComponent("ComboBox1"); ComboBox1.set("items", Select.join("\n"));
NOTE: I tried to add this in a loop, but it doesn't work.
for (i = 0; i < Select.length; i++) { ComboBox1.set("items", Select[i][0].join("\n")); }
-
@JulesV Do you mean you want the combo box to have the values
Alien, Cow, Bird, Shark
? Or do you want each of those in a separate combo box? -
@d-healey Yes, the combo box to have the values
Alien, Cow, Bird, Shark
. -
ComboBox1.setItems(""); // Clear the items for (x in Select) ComboBox1.addItem(x[0]); // Add the items
-
@d-healey I got it, Thank you sir!
-
-
@aaronventure Thanks, looks interesting :)