From 2843c5058cd3c54d507c9700ef435a9159443905 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Sun, 19 Sep 2021 00:14:28 +0900 Subject: Global_Objects/Object/entries の Markdown 化準備 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global_objects/object/entries/index.html | 155 --------------------- .../global_objects/object/entries/index.md | 155 +++++++++++++++++++++ 2 files changed, 155 insertions(+), 155 deletions(-) delete mode 100644 files/ja/web/javascript/reference/global_objects/object/entries/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/object/entries/index.md (limited to 'files/ja/web/javascript/reference') diff --git a/files/ja/web/javascript/reference/global_objects/object/entries/index.html b/files/ja/web/javascript/reference/global_objects/object/entries/index.html deleted file mode 100644 index 9b1787ea03..0000000000 --- a/files/ja/web/javascript/reference/global_objects/object/entries/index.html +++ /dev/null @@ -1,155 +0,0 @@ ---- -title: Object.entries() -slug: Web/JavaScript/Reference/Global_Objects/Object/entries -tags: - - JavaScript - - Method - - Object - - Reference - - メソッド -translation_of: Web/JavaScript/Reference/Global_Objects/Object/entries ---- -
{{JSRef}}
- -

Object.entries() メソッドは、引数に与えたオブジェクトが所有する、文字列をキーとした列挙可能なプロパティの組 [key, value] からなる配列を返します。配列要素の順序は {{jsxref("Statements/for...in", "for...in")}} ループによる順序と同じです (両者の主な違いは、for...in ループではプロトタイプチェーン内のプロパティも列挙されることです)。

- -

Object.entries() で返される配列の順序は、オブジェクトがどのように定義されたかに依存しません。特定の順序にする必要があるのであれば、Object.entries(obj).sort((a, b) => a[0] - b[0]); のようにして、まず配列を整列する必要があります。

- -
{{EmbedInteractiveExample("pages/js/object-entries.html", "taller")}}
- - - -

構文

- -
Object.entries(obj)
- -

引数

- -
-
obj
-
返されることになる文字列をキーとした列挙可能な所有プロパティの組 [key, value] を持つオブジェクト。
-
- -

返値

- -

引数に与えたオブジェクトが所有する、文字列をキーとした列挙可能なプロパティの組 [key, value] の配列。

- -

解説

- -

Object.entries() は、object に直接存在する文字列をキーとした列挙可能プロパティの組 [key, value] が配列要素に対応した配列を返します。プロパティの順序はオブジェクト内のプロパティに対してループさせた時の順序と同じになります。

- -

ポリフィル

- -

ネイティブで Object.entries に対応していない古い環境に互換性を持たせる場合は、以下のいずれかを使用することができます。

- - - -
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;
-  };
-}
-
- -

上記のポリフィルのコードスニペットで、IE<9 の対応が必要な場合、Object.keys の polyfill ({{jsxref("Object.keys")}} ページにあるようなもの) も必要となります。

- -

- -
const obj = { foo: 'bar', baz: 42 };
-console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
-
-// 配列様オブジェクト
-const obj = { 0: 'a', 1: 'b', 2: 'c' };
-console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
-
-// ランダムなキー順序を持つ配列様オブジェクト
-const anObj = { 100: 'a', 2: 'b', 7: 'c' };
-console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
-
-// 列挙可能でないプロパティ getFoo がある
-const myObj = Object.create({}, { getFoo: { value() { return this.foo; } } });
-myObj.foo = 'bar';
-console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]
-
-// オブジェクトでない引数はオブジェクトへと型強制されます
-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 への変換

- -

{{jsxref("Map", "new Map()")}} コンストラクターは entries による反復処理に対応しています。Object.entries を使うと、{{jsxref("Object")}} から {{jsxref("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 を使って、objects を簡単にループできます。

- -
const obj = { foo: 'bar', baz: 42 };
-Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
-
- -

仕様

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

ブラウザー実装状況

- -
- - -

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

-
- -

関連情報

- - diff --git a/files/ja/web/javascript/reference/global_objects/object/entries/index.md b/files/ja/web/javascript/reference/global_objects/object/entries/index.md new file mode 100644 index 0000000000..9b1787ea03 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/object/entries/index.md @@ -0,0 +1,155 @@ +--- +title: Object.entries() +slug: Web/JavaScript/Reference/Global_Objects/Object/entries +tags: + - JavaScript + - Method + - Object + - Reference + - メソッド +translation_of: Web/JavaScript/Reference/Global_Objects/Object/entries +--- +
{{JSRef}}
+ +

Object.entries() メソッドは、引数に与えたオブジェクトが所有する、文字列をキーとした列挙可能なプロパティの組 [key, value] からなる配列を返します。配列要素の順序は {{jsxref("Statements/for...in", "for...in")}} ループによる順序と同じです (両者の主な違いは、for...in ループではプロトタイプチェーン内のプロパティも列挙されることです)。

+ +

Object.entries() で返される配列の順序は、オブジェクトがどのように定義されたかに依存しません。特定の順序にする必要があるのであれば、Object.entries(obj).sort((a, b) => a[0] - b[0]); のようにして、まず配列を整列する必要があります。

+ +
{{EmbedInteractiveExample("pages/js/object-entries.html", "taller")}}
+ + + +

構文

+ +
Object.entries(obj)
+ +

引数

+ +
+
obj
+
返されることになる文字列をキーとした列挙可能な所有プロパティの組 [key, value] を持つオブジェクト。
+
+ +

返値

+ +

引数に与えたオブジェクトが所有する、文字列をキーとした列挙可能なプロパティの組 [key, value] の配列。

+ +

解説

+ +

Object.entries() は、object に直接存在する文字列をキーとした列挙可能プロパティの組 [key, value] が配列要素に対応した配列を返します。プロパティの順序はオブジェクト内のプロパティに対してループさせた時の順序と同じになります。

+ +

ポリフィル

+ +

ネイティブで Object.entries に対応していない古い環境に互換性を持たせる場合は、以下のいずれかを使用することができます。

+ + + +
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;
+  };
+}
+
+ +

上記のポリフィルのコードスニペットで、IE<9 の対応が必要な場合、Object.keys の polyfill ({{jsxref("Object.keys")}} ページにあるようなもの) も必要となります。

+ +

+ +
const obj = { foo: 'bar', baz: 42 };
+console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
+
+// 配列様オブジェクト
+const obj = { 0: 'a', 1: 'b', 2: 'c' };
+console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
+
+// ランダムなキー順序を持つ配列様オブジェクト
+const anObj = { 100: 'a', 2: 'b', 7: 'c' };
+console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
+
+// 列挙可能でないプロパティ getFoo がある
+const myObj = Object.create({}, { getFoo: { value() { return this.foo; } } });
+myObj.foo = 'bar';
+console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]
+
+// オブジェクトでない引数はオブジェクトへと型強制されます
+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 への変換

+ +

{{jsxref("Map", "new Map()")}} コンストラクターは entries による反復処理に対応しています。Object.entries を使うと、{{jsxref("Object")}} から {{jsxref("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 を使って、objects を簡単にループできます。

+ +
const obj = { foo: 'bar', baz: 42 };
+Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
+
+ +

仕様

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

ブラウザー実装状況

+ +
+ + +

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

+
+ +

関連情報

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