0

currently working with jquery object

the object contains data like

data[0]={CustUserID: 31, FirstName: "System12", LastName: "Administrator", CustUserName: "SysAdmin"}

this object contains n number of records,and we get the length using data.length

and also each object each record contains different type of keys and n number of keys with value

so now i am trying to get each key name and value name from each record and need to show on page.

on html view:

CustUserID=31
FirstName=System12
LastName=Administrator
CustUserName=SysAdmin

the code i wrote for this is

  var data="";
    for(var i=0;i<data.length;i++)
    {
    data= data+"</br>CustUserID="+data[i].CustUserID+
               "</br>FirstName="+data[i].FirstName+
               "</br>LastName="+data[i].LastName+
               "</br>CustUserName="+data[i].CustUserName;

    }

$("#DivData").html(data);

but i stucked when data keys are dynamically changing according to user requirment so at that i am facing problem to get data, so i need to get key names and data should be looped dynamically.

please help me...

thank you guys..

3 Answers 3

1

You can use jQuery .each()

var data_result = '';
//first loop will go trough all data array elements
$.each(data, function(key, data_element){
    // second loop will go trough all object keys
    $.each(data_element, function(key, value){
        data_result += '<br/>' + key + '= ' + value);
    });
});
$("#DivData").html(data_result );
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this without jQuery too

     var data = {
       CustUserID: 31,
       FirstName: "System12",
       LastName: "Administrator",
       CustUserName: "SysAdmin"
     };
     var dataHtml = '';

     for (var p in data) {
       if (data.hasOwnProperty(p)) {
         dataHtml += '<br/>' + p + "=" + data[p];
       }
     }

     $("#DivData").html(dataHtml);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="DivData"></div>

Comments

0
    var mydata = Array(), result;
    mydata[0]={CustUserID: 31, FirstName: "System12", LastName: "Administrator", CustUserName: "SysAdmin"};
    for (var key in mydata[0]){
        var obj = mydata[0][key];
        result += '<br/>'+key+'='+obj;
    }

jQuery("#myid").html(result);

Comments

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.