Ryan Litwiller Ryan Litwiller - 2 months ago 14
Jade Question

buiding a dropdown with json

Requirement :
I have a json response that I would like to transform to a

dropdown
. I'm using a jade template to handle the iteration but I'm having troubles getting the values. I'd like the sys_id be passed as a value on selecting the option(u_product_name) from the dropdown.

JS

result = //result is coming from API but this is what a console.log(result) shows
{ result:
[
{ sys_id: '7d950856',
u_product_name: 'ACCESS' },
{ sys_id: '803279e1',
u_product_name: 'AVAYA' },
{ sys_id: '87484c96',
u_product_name: 'ADAMO' },
{ sys_id: 'b3b9001a',
u_product_name: 'ADM' }
]
}


Closest thing i've gotten was using
JSON strigify
, although that split each letter as a new option.

Answer

Try this i hope it will work :

Html :

<select id="items"></select>

Script :

 var data={ result: 
                   [ 
                     { sys_id: '7d950856',
                       u_product_name: 'ACCESS' },
                     { sys_id: '803279e1',
                       u_product_name: 'AVAYA' },
                     { sys_id: '87484c96',
                       u_product_name: 'ADAMO' },
                     { sys_id: 'b3b9001a',
                       u_product_name: 'ADM' } 
                   ]
    };

var elemArr = data.result;
var select = document.getElementById( 'items' );

for (options in elemArr) {
select.add( new Option( elemArr[options].u_product_name) );
}

Working demo : https://jsfiddle.net/rohitjindal/ndfv8cy0/

Comments