-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Trick get a new copy of object/array using spread operator #1823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trick get a new copy of object/array using spread operator #1823
Conversation
3d6fa69
to
a2000fc
Compare
a2000fc
to
9cc9ad2
Compare
@@ -119,7 +119,7 @@ f(1); // 1 | |||
``` | |||
|
|||
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either. | |||
```` | |||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not change this. It corresponds to the symbol on line 107.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, fixed
@@ -225,6 +225,49 @@ But there's a subtle difference between `Array.from(obj)` and `[...obj]`: | |||
So, for the task of turning something into an array, `Array.from` tends to be more universal. | |||
|
|||
|
|||
## Get a new copy of an object/array | |||
|
|||
Remember when we talked about `Object.assign()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 230 and 231 should be in one line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally, github formatting does automatically a word wrap for one skipped line, the purpose was to keep easy to read code in the github editor
the javascript.info may not behave the same way, so i fixed it to one long line
## Get a new copy of an object/array | ||
|
||
Remember when we talked about `Object.assign()` | ||
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in) ? | |
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks
Remember when we talked about `Object.assign()` | ||
[in the past](https://javascript.info/symbol#symbols-are-skipped-by-for-in) ? | ||
|
||
It is possible to do the exact same thing with the spread operator ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to do the exact same thing with the spread operator ! | |
It is possible to do the exact same thing with the spread operator! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks
let arrCopy = [...arr]; // spread the array into a list of parameters | ||
// then put the result into a new array | ||
|
||
// do the arrays have the exact same contents ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// do the arrays have the exact same contents ? | |
// do the arrays have the exact same contents? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks
// do the arrays have the exact same contents ? | ||
alert(JSON.stringify(arr) === JSON.stringify(arrCopy)); // true (same content) | ||
|
||
// are the arrays equal (they share the same reference) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// are the arrays equal (they share the same reference) ? | |
// are the arrays equal (they share the same reference)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks
alert(arrCopy) // 1, 2, 3 | ||
``` | ||
|
||
Note that it is possible to do the exact same to copy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 252 and 253 should be in one line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as #1823 (comment)
// do the objects have the exact same contents ? | ||
alert(JSON.stringify(obj) === JSON.stringify(objCopy)); // true (same content) | ||
|
||
// are the arrays equal (they share the same reference) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// are the arrays equal (they share the same reference) ? | |
// are the arrays equal (they share the same reference)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks
alert(obj === objCopy); // false (not same reference) | ||
``` | ||
|
||
This way of copying an object is much shorter than |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 267 and 268 should be in one line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as #1823 (comment)
0c158d9
to
a54f242
Compare
Alexey -- is there a specific change you want me to look at? Everything I see above seems to be marked "outdated". In the a54f242 branch linked to in the last section there is some awkward language: 'Note that it is possible to do the exact same to copy of objects: ' could be improved: 'Note that it is possible to do the exact same thing to copy objects:' or 'Note that it is possible to do the exact same thing to make a copy of an object:' Rest of the language is OK. Anything else? |
a54f242
to
f4cb058
Compare
fixed, it's indeed clearer i removed the unneeded "exact" word :
|
Fine by me! |
@paroche did you accidentally hit the close PR instead of the merge PR button (just asking myself 🤔 ) |
You could say that. Anyway, as far as I'm concerned it should be merged (and closed). |
And it is. Not sure if it was my place, but if not, it can be undone. |
yes, you can always @ me if we need to change something |
A common trick using the spread operator :)
Credit goes to @Dorus for teaching me this trick !