简体   繁体   中英

How can I insert values in to javascript object by using jquery / javascript

Here i have and object and i want to insert the values in it dynamically.

      var mergTr={}; //object
       for(dd in dispID)
      {
        var dataID=displayObj[dispID[dd]]; 
        if(dataObj[dataID])
        {   var flag=0;  
            var valObj=dataObj[dataID];
            var Class2 = (clsNo==0)?"even":"odd";
            var cl_id=valObj["client_id"];
            var dataId=dataID;
            var columnName=valObj["fieldname"];
            var valFilterObj={0:"",1:"",2:columnName,3:dataObj[dataID]["filter_type"],4:"",5:dataObj[dataID]["filter_auto"]}; 
            if(flag==0){
                for(vv in valFilterObj){
                     tr=$("<tr/>");
                     mergTr=tr;
                     tr.addClass(clsNo);
                     var td=$("<td/>");
                     td.append(columnName);
                     tr.append(td);
                }
                $.merge(mergTr,tr);
            }
        }
      }

I want to insert 'tr' in object as looping.But i dont know how to do it. $.merge is not working. If i will directly assign the 'tr' then it replaces the old 'tr' value. Can anyone help me with this please.

If you need a list of <tr> you should have an array instead of object.

var mergTr={}; // {} denotes object
var mergTr=[]; // [] denotes array

You can push the values in array as mergTr.push(trObject);

You should use array to store tr and in for loop use var to redefine the tr locally then add to array.

var mergTr=[];

// the initial code 
// ...
       if(flag==0){
                for(vv in valFilterObj){
                  var tr=$("<tr/>");
                    // Here the old tr is being re-assigned  mergTr=tr;
                     tr.addClass(clsNo);
                     var td=$("<td/>");
                     td.append(columnName);
                     tr.append(td);
                    mergTr.push(tr);
                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM