-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-fromEntries.js
More file actions
40 lines (31 loc) · 930 Bytes
/
object-fromEntries.js
File metadata and controls
40 lines (31 loc) · 930 Bytes
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
//! The Object.fromEntries() method transforms a list of key-value pairs into an object.
//? With Object.fromEntries, you can convert from Map to Object
const entries = new Map([
["foo", "bar"],
["baz", 42],
]);
console.log(entries);
entries.set("killer", "healer");
const obj = Object.fromEntries(entries);
console.log(obj);
console.log("---------------------");
//! Converting an Array to an Object
const array = [
["khan", 2],
["talukder", 6],
];
const object = Object.fromEntries(array);
console.log(object);
console.log("---------------------");
//!invert key value pairs
const revertObj = Object.fromEntries(
Object.entries(obj).map(([key, value]) => [value, key])
);
console.log(revertObj);
console.log("---------------------");
//!transform objects
const obj1 = { x: 10, b: 9, f: 5 };
const obj2 = Object.fromEntries(
Object.entries(obj1).map(([key, value]) => [key, value * 2])
);
console.log(obj2);