Skip to content

Latest commit

 

History

History
111 lines (89 loc) · 3.45 KB

File metadata and controls

111 lines (89 loc) · 3.45 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Object.keys Function (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
Object.keys method [JavaScript]
keys method [JavaScript]
cf4a7daf-cf28-4467-bc6b-f7f106ec3876
12
mikejo5000
mikejo
ghogen

Object.keys Function (JavaScript)

Returns the names of the enumerable properties and methods of an object.

Syntax

Object.keys(object)  

Parameters

Parameter Definition
object Required. The object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

Return Value

An array that contains the names of the enumerable properties and methods of the object.

Exceptions

If the value supplied for the object argument is not the name of an object, a TypeError exception is thrown.

Remarks

The keys method returns only the names of enumerable properties and methods. To return the names of both enumerable and non-enumerable properties and methods, you can use Object.getOwnPropertyNames Function.

For information about the enumerable attribute of a property, see Object.defineProperty Function and Object.getOwnPropertyDescriptor Function.

Example

The following example creates an object that has three properties and a method. It then uses the keys method to get the properties and methods of the object.

// Create a constructor function.  
function Pasta(grain, width, shape) {  
    this.grain = grain;  
    this.width = width;  
    this.shape = shape;  
  
    // Define a method.  
    this.toString = function () {  
        return (this.grain + ", " + this.width + ", " + this.shape);  
    }  
}  
  
// Create an object.  
var spaghetti = new Pasta("wheat", 0.2, "circle");  
  
// Put the enumerable properties and methods of the object in an array.  
var arr = Object.keys(spaghetti);  
document.write (arr);  
  
// Output:  
// grain,width,shape,toString  

Example

The following example displays the names of all enumerable properties that start with the letter "g" in the Pasta object.

// Create a constructor function.  
function Pasta(grain, width, shape) {  
    this.grain = grain;  
    this.width = width;  
    this.shape = shape;  
}  
  
var polenta = new Pasta("corn", 1, "mush");  
  
var keys = Object.keys(polenta).filter(CheckKey);  
document.write(keys);  
  
// Check whether the first character of a string is "g".  
function CheckKey(value) {  
    var firstChar = value.substr(0, 1);  
    if (firstChar.toLowerCase() == "g")  
        return true;  
    else  
        return false;  
}  
  
// Output:  
// grain  

Requirements

[!INCLUDEjsv9]

See Also

Object.getOwnPropertyNames Function