8

A very basic question, but I can't find the solution :

I have a jquery object (given in console.log):

{ 
    id_ship: "1",  
    id_company: "1",  
    code: "DE",  
    // other properties...
}

I just want to get the first key on the object. In this case, i want to obtain id_ship

Thank you in advance

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

19

As @RoryMcCrossan has said, the order in object is not guranteed. But if you still want to get first key you can use:

Object.keys(obj)[0];
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • how if I have two object in array? when I want to get `id_ship` in the first object.. I try to use `Object.keys(obj[0])[0]` I try to console it and I got two.. how to get one? – Jems Dec 22 '17 at 03:14
  • @JamesRiady If you want to get the value of `id_ship`, you should use `arr[0].id_ship`. To get first key of first object in the array, use `Object.keys/(arr[0])[0]`. – Tushar Dec 22 '17 at 06:13