Object.keys()

You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

Object.keys() 메서드는 개체 고유 속성의 이름을 배열로 반환합니다. 배열 순서는 일반 반복문을 사용할 때와 같습니다.

구문

Object.keys(obj)

매개변수

obj
열거 가능한 고유 속성을 회수할 객체.

반환 값

주어진 객체의 모든 열거 가능한 속성의 이름을 나타내는 문자열 배열.

설명

Object.keys()object에서 직접 검색된 열거형 속성들에 대응하는 문자열을 요소로 갖는 배열을 반환합니다. 반환된 요소는 객체의 요소 전체를 수동으로 반복(loop)하며 주어지는 순서와 동일합니다.

예제

var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // ['2', '7', '100']

// getFoo is property which isn't enumerable
var myObj = Object.create({}, {
 getFoo: {
   value: function () { return this.foo; }
 } 
});
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']

비열거형(non-enumerables)을 포함한 모든 객체를 원한다면 Object.getOwnPropertyNames()를 참고하세요.

참고

In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError. In ES2015, a non-object argument will be coerced to an object.

Object.keys('foo');
// TypeError: "foo" is not an object (ES5 code)

Object.keys('foo');
// ["0", "1", "2"]                   (ES2015 code)

폴리필

To add compatible Object.keys support in older environments that do not natively support it, copy the following snippet:

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
  Object.keys = (function() {
    'use strict';
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function(obj) {
      if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
        throw new TypeError('Object.keys called on non-object');
      }

      var result = [], prop, i;

      for (prop in obj) {
        if (hasOwnProperty.call(obj, prop)) {
          result.push(prop);
        }
      }

      if (hasDontEnumBug) {
        for (i = 0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) {
            result.push(dontEnums[i]);
          }
        }
      }
      return result;
    };
  }());
}

Please note that the above code includes non-enumerable keys in IE7 (and maybe IE8), when passing in an object from a different window.

For a simple Browser Polyfill, see Javascript - Object.keys Browser Compatibility.

명세

Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'Object.keys' in that specification.
Standard Initial definition. Implemented in JavaScript 1.8.5.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Object.keys' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Object.keys' in that specification.
Draft  

브라우저 호환성

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
keysChrome Full support 5Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support

같이 보기