2

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>");  
}
1
  • Possible Duplicate. The question is kind of similar. Please refer Commented Jul 27, 2018 at 5:47

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to add full data as in option value ?
I guess you could add it as a string version of that object via JSON.stringify and then when you handle the selection you can transform back to object via JSON.parse ... but this is not a very good and elegant way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.