Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  164] [ 4]  / answers: 1 / hits: 22541  / 12 Years ago, mon, may 21, 2012, 12:00:00

I have this JSON:



var myVar = {
9:Automotive & Industrial,
1:Books,
7:Clothing
};


I want to add a new element at the beginning, I want to end up having this:




var myVar = {
5:Electronics,
9:Automotive & Industrial,
1:Books,
7:Clothing
};



I tried this but it is not working:



myVar.unshift({5:Electronics});


Thanks!


More From » json

 Answers
9

Just do it this way:



var myVar = {
9:Automotive & Industrial,
1:Books,
7:Clothing
};

// if you want to add a property, then...

myVar[5]=Electronics; // note that it won't be first or last, it's just 5

[#85442] Sunday, May 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;