回答編集履歴
2
stateClone
test
CHANGED
@@ -276,4 +276,48 @@
|
|
276
276
|
|
277
277
|
|
278
278
|
|
279
|
+
完全な deep copy ではなく、変数 `state` 特化のコードで良ければ、次のように書けます。
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
```JavaScript
|
284
|
+
|
285
|
+
const state = {
|
286
|
+
|
287
|
+
'001': [
|
288
|
+
|
289
|
+
{id: '001', score: 142},
|
290
|
+
|
291
|
+
{id: '001', score: 112}
|
292
|
+
|
293
|
+
],
|
294
|
+
|
295
|
+
'010': [
|
296
|
+
|
297
|
+
{id: '010', score: 142},
|
298
|
+
|
299
|
+
{id: '010', score: 112}
|
300
|
+
|
301
|
+
]
|
302
|
+
|
303
|
+
};
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
const stateClone = Object.entries(state).reduce((state, entry) => {
|
308
|
+
|
309
|
+
state[entry[0]] = entry[1].map(Object.assign.bind(null, {}));
|
310
|
+
|
311
|
+
return state;
|
312
|
+
|
313
|
+
}, state);
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
console.log(JSON.stringify(state) === JSON.stringify(stateClone)); // true
|
318
|
+
|
319
|
+
```
|
320
|
+
|
321
|
+
|
322
|
+
|
279
323
|
Re: hayatomo さん
|
1
参照透過性
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
### コード
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
シャローコピーで十分なような。
|
2
6
|
|
3
7
|
|
@@ -112,6 +116,142 @@
|
|
112
116
|
|
113
117
|
|
114
118
|
|
119
|
+
> 副作用がないというか、参照透過性?のある純粋関数を作りたいのです。
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```JavaScript
|
126
|
+
|
127
|
+
'use strict';
|
128
|
+
|
129
|
+
const payload = [
|
130
|
+
|
131
|
+
{id: '001', score: 100},
|
132
|
+
|
133
|
+
{id: '001', score: 40},
|
134
|
+
|
135
|
+
{id: '100', score: 132},
|
136
|
+
|
137
|
+
{id: '100', score: 482}
|
138
|
+
|
139
|
+
];
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
const state = {
|
144
|
+
|
145
|
+
'001': [
|
146
|
+
|
147
|
+
{id: '001', score: 142},
|
148
|
+
|
149
|
+
{id: '001', score: 112}
|
150
|
+
|
151
|
+
],
|
152
|
+
|
153
|
+
'010': [
|
154
|
+
|
155
|
+
{id: '010', score: 142},
|
156
|
+
|
157
|
+
{id: '010', score: 112}
|
158
|
+
|
159
|
+
]
|
160
|
+
|
161
|
+
};
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
var expectedResults = {
|
166
|
+
|
167
|
+
'100': [
|
168
|
+
|
169
|
+
{ id: '100', score: 132 },
|
170
|
+
|
171
|
+
{ id: '100', score: 482 }
|
172
|
+
|
173
|
+
],
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
'001': [
|
178
|
+
|
179
|
+
{ id: '001', score: 142 },
|
180
|
+
|
181
|
+
{ id: '001', score: 112 },
|
182
|
+
|
183
|
+
{ id: '001', score: 100 },
|
184
|
+
|
185
|
+
{ id: '001', score: 40 }
|
186
|
+
|
187
|
+
],
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
'010': [
|
192
|
+
|
193
|
+
{ id: '010', score: 142 },
|
194
|
+
|
195
|
+
{ id: '010', score: 112 }
|
196
|
+
|
197
|
+
]
|
198
|
+
|
199
|
+
};
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
function sample (payload, state) {
|
204
|
+
|
205
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
206
|
+
|
207
|
+
const results = JSON.parse(JSON.stringify(state));
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
for (let data of payload) {
|
212
|
+
|
213
|
+
const id = data.id;
|
214
|
+
|
215
|
+
data = Object.assign({}, data);
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if (!hasOwnProperty.call(state, id)) {
|
220
|
+
|
221
|
+
results[id] = [data];
|
222
|
+
|
223
|
+
} else {
|
224
|
+
|
225
|
+
results[id].push(data);
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
return results;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
const results = sample(payload, state);
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
console.log(JSON.stringify(state) === JSON.stringify(expectedResults)); // true
|
244
|
+
|
245
|
+
console.log(JSON.stringify(state)); // {"100":[{"id":"100","score":132},{"id":"100","score":482}],"001":[{"id":"001","score":142},{"id":"001","score":112},{"id":"001","score":100},{"id":"001","score":40}],"010":[{"id":"010","score":142},{"id":"010","score":112}]}
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
### Deep Copy
|
252
|
+
|
253
|
+
|
254
|
+
|
115
255
|
> //(1)Deep Copyするには?(ライブラリで解決する方法は既に把握済みですが、ライブラリを使わない場合はどう書くのか?
|
116
256
|
|
117
257
|
|