#dev/개념정리

JS: Object.keys() & Object.values() & Object.entries()

lunacer 2020. 5. 12. 17:59

Object.keys()

1. method 
2. iterated in the same order that a normal loop would.

 

[Syntax]

Object.keys(obj)

 

[Return Value]

An array 
: of strings 
: of a given object's own enumerable property names
: that represent all the enumerable properties of the given object.
: The ordering of the properties is the same as the obj.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

 


Object.values()  

1. method
2. in the same order as that provided by a for...in loop.
3. (The only difference is that a for...in loop enumerates properties in the prototype chain as well.)

 

[Syntax]

Object.values(obj)

 

[Return Value]

An array 
: containing the obj's own enumerable property values.

: The ordering of the properties is the same as that given by looping over the property values of the object manually.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

 


Also, check this article:
https://gomakethings.com/why-isnt-there-an-object.foreach-method/


Object.entries()

1. method
2. in the same order as that provided by a for...in loop.
3. (The only important difference is that a for...in loop enumerates properties in the prototype chain as well).
4. The order of the array returned by Object.entries() does not depend on how an object is defined. 
   : If there is a need for certain ordering, then the array should be sorted first.
      - Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));

 

[Syntax]

Object.entries(obj)

 

[Return Value]

An array 
: of the obj's own enumerable string-keyed property [key, value] pairs.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries