From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/object/values/index.html | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/object/values/index.html (limited to 'files/ja/web/javascript/reference/global_objects/object/values') diff --git a/files/ja/web/javascript/reference/global_objects/object/values/index.html b/files/ja/web/javascript/reference/global_objects/object/values/index.html new file mode 100644 index 0000000000..d016cb025c --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/object/values/index.html @@ -0,0 +1,98 @@ +--- +title: Object.values() +slug: Web/JavaScript/Reference/Global_Objects/Object/values +tags: + - JavaScript + - Method + - Object + - Reference + - メソッド +translation_of: Web/JavaScript/Reference/Global_Objects/Object/values +--- +
{{JSRef}}
+ +

Object.values() メソッドは、指定されたオブジェクトが持つ列挙可能なプロパティの値を、 {{jsxref("Statements/for...in", "for...in")}} ループで提供される場合と同じ順序で配列にして返します。 (違いは、 for...in ループではプロパティチェーン上のプロパティも同様に列挙するという点だけです。)

+ +
{{EmbedInteractiveExample("pages/js/object-values.html")}}
+ + + +

構文

+ +
Object.values(obj)
+ +

引数

+ +
+
obj
+
返されることになる列挙可能な自身のプロパティの値を持つオブジェクト。
+
+ +

返値

+ +

与えられたオブジェクトが所有する列挙可能なプロパティの値が入った配列。

+ +

解説

+ +

Object.values() は、object に直接存在する列挙可能な値が配列要素の文字列に対応した配列を返します。プロパティの順序はマニュアル操作でオブジェクト内のプロパティに対してループさせた時の順序と同じになります。

+ +

使用例

+ +
const obj = { foo: 'bar', baz: 42 };
+console.log(Object.values(obj)); // ['bar', 42]
+
+// 配列風オブジェクト
+const arrayLikeObj1 = { 0: 'a', 1: 'b', 2: 'c' };
+console.log(Object.values(arrayLikeObj1 )); // ['a', 'b', 'c']
+
+// ランダムなキー順序を持つ配列風オブジェクト
+// 数値のキーを使用すると、値はキーの番号順に返される
+const arrayLikeObj2 = { 100: 'a', 2: 'b', 7: 'c' };
+console.log(Object.values(arrayLikeObj2 )); // ['b', 'c', 'a']
+
+// 列挙可能でないプロパティ getFoo がある
+const my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } });
+my_obj.foo = 'bar';
+console.log(Object.values(my_obj)); // ['bar']
+
+// オブジェクトでない引数はオブジェクトへと型強制される
+console.log(Object.values('foo')); // ['f', 'o', 'o']
+
+ +

ポリフィル

+ +

Object.values に対応していない古い環境と互換性を持たせる場合は、 tc39/proposal-object-values-entrieses-shims/Object.values 内にポリフィルがあります。

+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-object.values', 'Object.values')}}
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("javascript.builtins.Object.values")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf