JQuery Key and Value

{
    "clientIds": [{ 
        "id": 0,
        "clientId": "check123"
    }]
}

      

The above json object. How do you break this down into key values? I have a key: clientIds but the value comes in an object, I want the check123 value and add in the dropdown using jquery how to split and add in the dropdown.

+3


source to share


5 answers


You can access clientId

and Id

how to



var a = {"clientIds":[{"id":0,"clientId":"check123"}]};
alert(a.clientIds[0].clientId);
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      

Run codeHide result


+1


source


To take an object and include it in the options in the selection menu, do the following.



for(var i = 0; i < clientIds.length; i++){
   $('#dropdown').append($('<option />').val(clientIds[i].id)).text(clientIds[i].clientId));
}

      

+1


source


Since it clientIds

is an array, iterate over it.

 var data = {"clientIds":[{"id":0,"clientId":"check123"}]};
 $.each(data, function(index, element) {
    var id = element.id; // will have the value 0
     var clientId = element.clientId; // will have the value check123
 });

      

Once you get them, create a dropdown menu.

+1


source


   var a = {"clientIds":[{"id":0,"clientId":"check123"}]};
       $.each( a.clientIds,function () {
          $('#dropdown').append($('<option />').val($(this).id)).text($(this).clientId);
       });

      

+1


source


Try the following:

var a = {"clientIds":[{"id":0,"clientId":"check123"}]};
for(i=0;i<a.clientIds.length;i++)
    $('#myoptions').append($('<option value="'+a.clientIds[i].id+'">'+a.clientIds[i].clientId+'</option>'));

      

DEMO FIDDLE

+1


source







All Articles