0

I am having JSON object

var jsonData={
  "key1":val1,
  "key2":val2
}

I am having array

var columns=[100,101,102,103];
var resultarray=[{C_100:1,C_101:2,102:3,C_103:4},{C_100:5,C_101:6,C_102:7,C_103:8};

I am looping through result array to get create json object matching the values of columns

for(let i=0;i<resultarray.length; i++)
{
  var finalRes=resultarray[i];      
  for (let column = 0; column < columns.length; column++) {                            
    dataVal = finalRes["C_"+columns[column]];                          
    jsonData[columns[column]] = dataVal;                                
  }    
}

I want to add json key value pairs in the lopp to existing json object already created.

I am getting jsonObject output as

var jsonData=
{
  100:1,
  101:2,
  102:3,
  key1:val1,
  key2:val2,
}

Which will not work for my scenario

I am expecting json object as

var jsonData=
{    
  key1:val1,
  key2:val2,
  100:1,
  101:2,
  102:3,
}

Please let me know how to push items to existing json object which should append at the end;

AMDI
  • 895
  • 2
  • 17
  • 40

1 Answers1

0

I tried this way and it worked. I have appended c_ for each column and evrything is string it got appeneded correctly in order.

var columns=[C_100,C_101,C_102,C_103];

for(let i=0;i<resultarray.length; i++)
{
  var finalRes=resultarray[i];      
  for (let column = 0; column < columns.length; column++) {                            
    dataVal = finalRes[columns[column]];                          
    jsonData[""+columns[column]""] = dataVal;                                
  }    
}

Now i got out put of JsonData as

var jsonData=
{    
  key1:val1,
  key2:val2,
  C_100:1,
  C_101:2,
  C_102:3,
  C_103:4
}

No i have to work around to remove "C_" from key

AMDI
  • 895
  • 2
  • 17
  • 40