Sommario
Il metodo Object.keys()
restituisce un array contenente le proprietà enumerabili di un dato oggetto, nel medesimo ordine fornito da un ciclo for...in
(la differenza è che un ciclo for-in enumera anche le proprietà nella catena di prototipi).
Sintassi
Object.keys(obj)
Parametri
- obj
- L'oggetto del quale si devono restituire le proprietà enumerabili.
Descrizione
Object.keys
restituisce un array i quali elementi sono stringhe corrispondenti alle proprietà enumerabili trovate direttamente in obj
. L'ordine delle proprietà è lo stesso di quello dato ciclando manualmente sulle proprietà dell'oggetto.
Esempi
var arr = ["a", "b", "c"];
alert(Object.keys(arr)); // chiama alert con argomento "0,1,2"
// array like object
var obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // chiama alert con argomento "0,1,2"
// array like object with random key ordering
var an_obj = { 100: "a", 2: "b", 7: "c"};
alert(Object.keys(an_obj)); // chiama alert con argomento "2, 7, 100"
// getFoo is property which isn't enumerable
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
my_obj.foo = 1;
alert(Object.keys(my_obj)); // chiama alert con foo come unico argomento
Per ottenere tutte le proprietà, anche quelle non enumerabili, si veda Object.getOwnPropertyNames
.
Polyfill
Per aggiungere un supporto equivalente a Object.keys,
in ambienti datati che non lo supportino nativamente, si copi il seguente frammento di codice:
// Da 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 !== 'object' && (typeof obj !== 'function' || 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;
};
}());
}
Si noti che il codice sopra include chiavi non-enumerabili in IE7 (e forse IE8), nel caso in cui si passi un oggetto proveniente da un'altra finestra.
Per un semplice polyfill, si veda Javascript - Object.keys Browser Compatibility.
Specifiche
Specifica | Stato | Commento |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Object.keys' in that specification. |
Standard | Definizione iniziale. implementato in in JavaScript 1.8.5 |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.keys' in that specification. |
Standard |
Compatibilità dei browser
Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Supporto base | 4 (2.0) | 5 | 9 | 12 | 5 |
Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Supporto base | ? | ? | ? | ? | ? |
Basato su Kangax's compat table.