0

I have a JSON object in session :

{ "Zone" : "Bangalore" , "Role" : "1" , "Vehicles" : [ "v123" , "v345" , "v567"]}

I need to get the values of Vehicles in ArrayList and pass to a selectBox with multiple option

Am trying something like this:

 var temp = sessionStorage.getItem('userDetails');
    var viewName = $.parseJSON(temp);
    alert(viewName.Vehicles);
    var v1 = new Array(viewName.Vehicles);
    for (var i=0; i < v1.length;++i){
        alert("");
        addOption(document.myform1.v1, v1[i], v1[i]);
        }

function addOption(selectbox,text,value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

I am getting the values of

alert(viewName.Vehicles);

as v123,v234,v345. But I need to convert var veh as ArrayList to pass to the selectbox

The code to convert it to ArrayList is not working.

1
  • What you want do actully as v1 is an array already? Commented Feb 15, 2014 at 13:55

1 Answer 1

2

When you parse the your json, viewName.Vehicles is already an array.

for (var i=0; i < viewName.Vehicles.length;++i){
    addOption(document.myform1[viewName.Vehicles[i]], viewName.Vehicles[i], viewName.Vehicles[i]);
}

you can check it like:

Object.prototype.toString.call(viewName.Vehicles)

the result is:

"[object Array]"

or try:

viewName.Vehicles instanceof Array
Sign up to request clarification or add additional context in comments.

5 Comments

but when i pass addOption(document.myform1.viewName.Vehicles, viewName.Vehicles[i], viewName.Vehicles[i]); It fails..
@user2093576: I think what you want to do is document.myform1[viewName.Vehicles[i]]
@user2093576: are these your field names "v123" , "v345" , "v567" ?
values of the key - "Vehicles" : [ "v123" , "v345" , "v567"]
You were absolutely right.. addOption(document.myform1.Vehicle_list, viewName.Vehicles[i], viewName.Vehicles[i]); working fine

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.