當前位置:生活全書館 >

IT科技

> values()的用法 object

values()的用法 object

Object.values()返回一個數組,其元素是在物件上找到的可列舉屬性值。屬性的順序與通過手動迴圈物件的屬性值所給出的順序相同。

object.values()的用法

語法:

Object.values(obj)

引數:

obj:被返回可列舉屬性值的物件。

返回值:

一個包含物件自身的所有可列舉屬性值的陣列。

示例:

var obj = { foo: 'bar', baz: 42 };console.log(Object.values(obj)); // ['bar', 42]// array like objectvar obj = { 0: 'a', 1: 'b', 2: 'c' };console.log(Object.values(obj)); // ['a', 'b', 'c']// array like object with random key ordering// when we use numeric keys, the value returned in a numerical order according to the keysvar an_obj = { 100: 'a', 2: 'b', 7: 'c' };console.log(Object.values(an_obj)); // ['b', 'c', 'a']// getFoo is property which isn't enumerablevar 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 objectconsole.log(Object.values('foo')); // ['f', 'o', 'o']

object.values()的用法 第2張

標籤: objectvalues
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/25r30k.html