bmtc its
bmtc its

Reputation: 23

how to append dropdown option value as an array

My json looks like this.I need to append name as key and data as value in dropdown option .

   {
    "id" : 1,
    "name" : "name1",
    "Year" : 2018,
    "data" : [ 
        {
            "day" : 1.0,
            "count" : 34.0,
            "month" : "May",
            "price" : 1.0,
            "amount" : 51.0
        }, 
        {
            "day" : 2.0,
            "count" : 34.0,
            "month" : "May ",
            "price_typ" : 1.0,
            "amount" : 51.5
        }, 
        {
            "day" : 3.0,
            "count" : 34.0,
            "month" : "May",
            "price_typ" : 1.0,
            "amount" : 50.0
        }, 
        {
            "day" : 4.0,
            "count" : 34.0,
            "month" : "May",
            "price_typ" : 1.0,
            "amount" : 51.0
        }, 
        {
            "day" : 5.0,
            "count" : 34.0,
            "month" : "May",
            "price_typ" : 1.0,
            "amount" : 51.0
        }
    ]
}

In my code i append name as key and data as option value but am getting [object,object] instead of array values.

for(var j=0;j<result.length;j++){
$("#custom-dropdown").append(
"<option value="+result[j].data+">"
+ result[j].name
+ "</option>");  
}

Upvotes: 2

Views: 93

Answers (1)

Akrion
Akrion

Reputation: 18515

You can not do "<option value="+result[j].data+">" since you are telling JS to unbox result[j].data as a string and since that is an object JS would tell you that it is [object, object]

Simple test:

var a = {}; 
a.toString() // returns "[object Object]"

Which part/property value from data do you need as value?

Upvotes: 4

Related Questions