網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

values()的用法|object

欄目: IT科技 / 發佈於: / 人氣:2.49W

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張

Tags:objectvalues