Object.keys()The For-in method returns an array of its own enumerable properties for a given object, the order in which the property names are arranged in the array, and the order in which they are for...in returned when iterating through the object (the main difference between them is that a single loop also enumerates the properties on its prototype chain).
Grammar
Object.keys(obj)
Parameters
-
Obj
-
the object to return the property to which it enumerates itself.
return value
An array of strings representing all the enumerable properties of the given object.
Describe
Object.keysReturns an array of all elements that are strings, and whose elements come from a object property that can be directly enumerated from the given above. The order of these properties is consistent with the manual traversal of the object's properties.
Example
Simple arrayvar arr=[A,' B ',C]; console.Log(Object.Keys(arr));Console: [' 0 ', ' 1 ', ' 2 ']Array like objectvar obj={0:A,1:' B ',2:C}; console.Log(Object.Keys(obj));Console: [' 0 ', ' 1 ', ' 2 ']Array like object with random key orderingvar anobj={100:A,2:' B ',7:C}; console.Log(Object.Keys(Anobj));Console: [' 2 ', ' 7 ', ' 100 ']Getfoo is a property which isn ' t enumerablevar myObj= Object.Create({},{Getfoo:{Value:function(){return this.foo } };myobj.foo = 1;console log (Object.keys (Myobj) //console: [' foo ']
If you want to get all the properties of an object, even including non-enumerable, see Object.getOwnPropertyNames .
Get method
data[Object.keys(data)[Object.keys(data).length - 1]]
Attention
In ES5, if the parameter of this method is not an object (but a primitive value), then it throws TypeError. In ES2015, parameters of non-objects are cast to an object.
Object.keys("foo");// TypeError: "foo" is not an object (ES5 code)Object.keys("foo");// ["0", "1", "2"] (ES2015 code)
JS gets the last one of the objects