0
var data= [{"key1":"3525","key2":"3526","key3":"3527"}];

i tried with assigning property value to object as follows.

for(var i = 0;i<data.length;i++){
                    data[i]["slno"]=i+1;
                }

But it results like this data= {"key1":"3525","key2":"3526","key3":"3527","slno":"1"};

But Actually i need like below format. It means slno should appear in first instead of last.

var json = {"slno":"1","key1":"3525","key2":"3526","key3":"3527"};

1 Answers1

1

Objects in javascript are unordered, so you cannot control the order of them. You can however use an array.

Example:

arr = [{key:"key1", val: 3525},{key:"key2", val: 3526}];

arr.unshift({ key: 'key3', value: 3527 });

Hope this helps!

N. Ivanov
  • 1,757
  • 2
  • 12
  • 26
  • unshift is used for array but i need to do it for object – Akshay Uttur Oct 24 '17 at 05:23
  • @AkshayUttur I have explicitly mentioned that objects are *UNORDERED* and cannot be moved around, hence the suggestion for use of an array. This was mentioned in other comments as well, and this tells me that you do not read, nor appreciate the help of fellow SO users. If you do not put any effort in to the help of the rest what is the point of using SO? Thanks – N. Ivanov Oct 24 '17 at 07:44
  • lvanov First i apologise for my above comment. Yeah what you said is correct but i want only for object, so u have any other ideas? – Akshay Uttur Oct 24 '17 at 12:09
  • @AkshayUttur you can never guarantee the order even within 1 object, so no sorry I do not have any other ideas. Maybe research more and you will find other ways? Thanks – N. Ivanov Oct 24 '17 at 12:28