aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/global_objects/object/fromentries/index.html
blob: 4d74fc326accbdbb6bf57cb1b7aacd3e8385ab11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
title: Object.fromEntries()
slug: Web/JavaScript/Reference/Global_Objects/Object/fromEntries
tags:
  - JavaScript
  - Method
  - Object
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Object/fromEntries
---
<div>{{JSRef}}</div>

<p><code><strong>Object.fromEntries()</strong></code> 메서드는 키값 쌍의 목록을 객체로 바꿉니다.</p>

<div>{{EmbedInteractiveExample("pages/js/object-fromentries.html")}}</div>

<h2 id="구문">구문</h2>

<pre class="syntaxbox">Object.fromEntries(<var>iterable</var>);</pre>

<h3 id="매개변수">매개변수</h3>

<dl>
 <dt><code>iterable</code></dt>
 <dd>반복 가능한 객체. 즉, {{jsxref("Array")}}{{jsxref("Map")}} 또는 <a href="/ko/docs/Web/JavaScript/Reference/Iteration_protocols">반복 규약</a>을 구현한 기타 객체.</dd>
</dl>

<h3 id="반환_값">반환 값</h3>

<p>속성의 키와 값을 반복 가능한 객체에서 가져온 새로운 객체.</p>

<h2 id="설명">설명</h2>

<p><code>Object.fromEntries()</code> 메서드는 키값 쌍 목록을 받고, 그 목록을 사용해 속성을 부여한 새로운 객체를 반환합니다. <code>iterable</code> 인자는 <code>@@iterator</code> 메서드를 구현하여 반복기 객체를 반환해야 하고, 그 반복기는 또 배열 형태의 객체로 요소 2개를 반환해야 합니다. 반환된 요소 중 첫 번째는 생성할 객체의 속성 키로, 두 번째는 속성 값으로 사용합니다.</p>

<p><code>Object.fromEntries()</code>{{jsxref("Object.entries()")}}의 역을 수행합니다.</p>

<h2 id="예제">예제</h2>

<h3 id="Map에서_Object로"><code>Map</code>에서 <code>Object</code></h3>

<pre class="brush: js">const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);
const obj = Object.fromEntries(map);
console.log(obj); // { foo: "bar", baz: 42 }
</pre>

<h3 id="Array에서_Object로"><code>Array</code>에서 <code>Object</code></h3>

<pre class="brush: js">const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }
</pre>

<h3 id="객체_변환">객체 변환</h3>

<p><code>Object.fromEntries()</code>와 그 역인 {{jsxref("Object.entries()")}}, 그리고 <a href="/ko/docs/Web/JavaScript/Reference/Global_Objects/Array#메서드_2">배열 변형 메서드</a>를 통해 객체를 변환할 수 있습니다.</p>

<pre class="brush: js">const object1 = { a: 1, b: 2, c: 3 };

const object2 = Object.fromEntries(
  Object.entries(object1)
  .map(([ key, val ]) =&gt; [ key, val * 2 ])
);

console.log(object2);
// { a: 2, b: 4, c: 6 }</pre>

<h2 id="명세">명세</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><a href="https://tc39.github.io/proposal-object-from-entries/">https://tc39.github.io/proposal-object-from-entries</a></td>
   <td>Stage 3</td>
  </tr>
 </tbody>
</table>

<h2 id="브라우저_호환성">브라우저 호환성</h2>

<p>{{Compat("javascript.builtins.Object.fromEntries")}}</p>

<h2 id="같이_보기">같이 보기</h2>

<ul>
 <li>{{jsxref("Object.entries()")}}</li>
 <li>{{jsxref("Object.keys()")}}</li>
 <li>{{jsxref("Object.values()")}}</li>
 <li>{{jsxref("Map.prototype.entries()")}}</li>
 <li>{{jsxref("Map.prototype.keys()")}}</li>
 <li>{{jsxref("Map.prototype.values()")}}</li>
</ul>