JS gets the last one of the objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.