Object.entries()
method returns an array of a given object's own enumerable string-keyed property
[
key
,
value
]
pairs, in the same order as that provided by a
for...in
loop. (The only important difference is that a
for...in
loop enumerates properties in the prototype chain as well).
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, like
Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Object.entries(obj)
obj
[
key
,
value
]
pairs are to be returned.
An array of the given object's own enumerable string-keyed property
[
key
,
value
]
pairs.
Object.entries()
returns an array whose elements are arrays corresponding to the enumerable string-keyed property
[
key
,
value
]
pairs found directly upon
object
. The ordering of the properties is the same as that given by looping over the property values of the object manually.
To add compatible
Object.entries()
support in older environments that do not natively support it, you can use any of the following:
Object.entries
在
tc39/proposal-object-values-entries
(if you don't need any support for IE);
if (!Object.entries) {
Object.entries = function( obj ){
var ownProps = Object.keys( obj ),
i = ownProps.length,
resArray = new Array(i); // preallocate the Array
while (i--)
resArray[i] = [ownProps[i], obj[ownProps[i]]];
return resArray;
};
}
For the above polyfill code snippet, if you need support for IE<9, then you will also need an
Object.keys()
polyfill (such as the one found on the
Object.keys
page).
const obj = { foo: 'bar', baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
// getFoo is property which isn't enumerable
const myObj = Object.create({}, { getFoo: { value() { return this.foo; } } });
myObj.foo = 'bar';
console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]
// non-object argument will be coerced to an object
console.log(Object.entries('foo')); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
// returns an empty array for any primitive type, since primitives have no own properties
console.log(Object.entries(100)); // [ ]
// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for (const [key, value] of Object.entries(obj)) {
console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
}
// Or, using array extras
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
});
Object
到
Map
new Map()
constructor accepts an iterable of
entries
. With
Object.entries
, you can easily convert from
Object
to
Map
:
const obj = { foo: 'bar', baz: 42 };
const map = new Map(Object.entries(obj));
console.log(map); // Map { foo: "bar", baz: 42 }
Object
使用 Array Destructuring , you can iterate through objects easily.
const obj = { foo: 'bar', baz: 42 };
Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.entries' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
entries
|
Chrome 54 | Edge 14 | Firefox 47 | IE No | Opera 41 | Safari 10.1 | WebView Android 54 | Chrome Android 54 | Firefox Android 47 | Opera Android 41 | Safari iOS 10.3 | Samsung Internet Android 6.0 |
nodejs
7.0.0
|
完整支持
不支持
用户必须明确启用此特征。
Object.keys()
Object.values()
Object.prototype.propertyIsEnumerable()
Object.create()
Object.fromEntries()
Object.getOwnPropertyNames()
Map.prototype.entries()
Map.prototype.keys()
Map.prototype.values()
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function