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.

Answers 1

  • 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);
                    }
    
                }
    

Related Articles