Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  158] [ 7]  / answers: 1 / hits: 33353  / 11 Years ago, thu, august 22, 2013, 12:00:00

I'm looking to stringify an object.



I want in output like this



{1:{valeur:dalebrun,usager:experttasp,date:2013-08-20 16:41:50}, 2: {valeur:test,usager:experttasp,date:2013-08-20 16:41:50}}


But I get this



{valeur:dalebrun,usager:experttasp,date:2013-08-20 16:41:50}, {valeur:test,usager:experttasp,date:2013-08-20 16:41:50}


What I do



var objVal = {}; //value....
var data = {}; //other value....
var object = $.extend({}, objVal, data); //concat the object
JSON.stringify(object);

More From » json

 Answers
60

I found the solution !



I do an for loop on the object. And I iterate on each element in the object. Thank you for your help. The answer of @Giovanni help me to found the solution.



Solution:



var data = {}; //values....
var objVal = {}; //other values....
var final = {};
var index = 1;
for(var key in data)
{
final[index] = data[key];
index = index + 1;
}
final[index] = objVal;
JSON.stringify(final);


And the output is :



{1:{valeur:dfgdfg,usager:experttasp,date:2013-08-23 10:36:54},2:{valeur:uuuuuuuuuu,commentaire:defg,usager:experttasp,date:2013-08-23 10:37:26},3:{valeur:uuuuuuuuuu,commentaire:yesssss,usager:experttasp,date:2013-08-23 10:38:38}}

[#76208] Wednesday, August 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samaraanandah

Total Points: 94
Total Questions: 86
Total Answers: 99

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;