Object.values()
method returns an array of a given object's own enumerable property values, in the same order as that provided by a
for...in
loop. (The only difference is that a
for...in
loop enumerates properties in the prototype chain as well.)
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.values(obj)
obj
The object whose enumerable own property values are to be returned.
An array containing the given object's own enumerable property values.
Object.values()
returns an array whose elements are the enumerable property values found on the 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.values
support in older environments that do not natively support it, you can find a Polyfill in the
tc39/proposal-object-values-entries
or in the
es-shims/Object.values
repositories.
const obj = { foo: 'bar', baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]
// Array-like object
const arrayLikeObj1 = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(arrayLikeObj1 )); // ['a', 'b', 'c']
// Array-like object with random key ordering
// When using numeric keys, the values are returned in the keys' numerical order
const arrayLikeObj2 = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.values(arrayLikeObj2 )); // ['b', 'c', 'a']
// getFoo is property which isn't enumerable
const my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } });
my_obj.foo = 'bar';
console.log(Object.values(my_obj)); // ['bar']
// non-object argument will be coerced to an object
console.log(Object.values('foo')); // ['f', 'o', 'o']
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.values' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
值
|
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.entries()
Object.prototype.propertyIsEnumerable()
Object.create()
Object.getOwnPropertyNames()
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