-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.Object-to-array.js
More file actions
44 lines (35 loc) · 1012 Bytes
/
7.Object-to-array.js
File metadata and controls
44 lines (35 loc) · 1012 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
41
42
43
44
/*
* Author: Marc Ciruelos Santos
* Date: 02-03-2024
* Description: Object to Array => Use of Object.keys(), Object.values() and Object.entries()
*/
const book = {
id: 1,
title: "Thinking, fast and slow",
author: "Daniel Kahneman",
price: 12.5,
format: "paperback",
genres: ["Education", "Psychology"],
pages: 731,
};
// Object.keys()
const properties = Object.keys(book);
console.log(properties.length); // 7
properties.forEach((property) => {
// id, title, author...
console.log("Object.keys(): " + property);
});
// Object.values()
const values = Object.values(book);
values.forEach((value) => {
// 1, "Thinking, fast and slow", ..., ["Education", "Psychology"]...
console.log("Object.values(): " + value);
});
// Object.entries() => returns a bidimensional array (key, value)
const entries = Object.entries(book);
console.log("");
console.log("Object.entries(): ");
entries.forEach((entry) => {
// ['id', 1], ['title', 'Thinking, fast and slow']
console.log(entry);
});