-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.rest_spread.js
More file actions
174 lines (161 loc) · 3.97 KB
/
2.rest_spread.js
File metadata and controls
174 lines (161 loc) · 3.97 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// REST OPERATOR
const data = [
{
id: 1,
title: "The Lord of the Rings",
publicationDate: "1954-07-29",
author: "J. R. R. Tolkien",
genres: [
"fantasy",
"high-fantasy",
"adventure",
"fiction",
"novels",
"literature",
],
hasMovieAdaptation: true,
pages: 1216,
translations: {
spanish: "El señor de los anillos",
chinese: "魔戒",
french: "Le Seigneur des anneaux",
},
reviews: {
goodreads: {
rating: 4.52,
ratingsCount: 630994,
reviewsCount: 13417,
},
librarything: {
rating: 4.53,
ratingsCount: 47166,
reviewsCount: 452,
},
},
},
{
id: 2,
title: "The Cyberiad",
publicationDate: "1965-01-01",
author: "Stanislaw Lem",
genres: [
"science fiction",
"humor",
"speculative fiction",
"short stories",
"fantasy",
],
hasMovieAdaptation: false,
pages: 295,
translations: {},
reviews: {
goodreads: {
rating: 4.16,
ratingsCount: 11663,
reviewsCount: 812,
},
librarything: {
rating: 4.13,
ratingsCount: 2434,
reviewsCount: 0,
},
},
},
{
id: 3,
title: "Dune",
publicationDate: "1965-01-01",
author: "Frank Herbert",
genres: ["science fiction", "novel", "adventure"],
hasMovieAdaptation: true,
pages: 658,
translations: {
spanish: "",
},
reviews: {
goodreads: {
rating: 4.25,
ratingsCount: 1142893,
reviewsCount: 49701,
},
},
},
{
id: 4,
title: "Harry Potter and the Philosopher's Stone",
publicationDate: "1997-06-26",
author: "J. K. Rowling",
genres: ["fantasy", "adventure"],
hasMovieAdaptation: true,
pages: 223,
translations: {
spanish: "Harry Potter y la piedra filosofal",
korean: "해리 포터와 마법사의 돌",
bengali: "হ্যারি পটার এন্ড দ্য ফিলোসফার্স স্টোন",
portuguese: "Harry Potter e a Pedra Filosofal",
},
reviews: {
goodreads: {
rating: 4.47,
ratingsCount: 8910059,
reviewsCount: 140625,
},
librarything: {
rating: 4.29,
ratingsCount: 120941,
reviewsCount: 1960,
},
},
},
{
id: 5,
title: "A Game of Thrones",
publicationDate: "1996-08-01",
author: "George R. R. Martin",
genres: ["fantasy", "high-fantasy", "novel", "fantasy fiction"],
hasMovieAdaptation: true,
pages: 835,
translations: {
korean: "왕좌의 게임",
polish: "Gra o tron",
portuguese: "A Guerra dos Tronos",
spanish: "Juego de tronos",
},
reviews: {
goodreads: {
rating: 4.44,
ratingsCount: 2295233,
reviewsCount: 59058,
},
librarything: {
rating: 4.36,
ratingsCount: 38358,
reviewsCount: 1095,
},
},
},
];
function getBooks(id){
return data.find((d) => d.id ==id);
}
const bookThree = getBooks(3)
const { pages, genres} = bookThree;
//const primaryGenre = genres[0]
//const secondaryGenre = genres[1]
const [primaryGenre, secondaryGenre, ...otherGenres] = genres;
primaryGenre;
secondaryGenre;
otherGenres;
// after destructuring an array and access the first and second item, the rest of the items are stored in a variable we assign using (...)
// it is placed at the end of the destructing operation
//SPREAD OPERATOR///
//this is also used on arrays and objects
const newGenres = [...genres, 'epic fantasy']; //at the end
const newGenresStart = [ 'epic fantasy', ...genres] //at the start
newGenres;
newGenresStart
//the spread operator adds a new item to an array or object.
//the operator is instanciated by using (...)
const updatedBook = {...bookThree, moviePublicationDate: '2025-08-02', pages: 2025};
updatedBook;
//in objects you can either add a new property or overide an existing one