Skip to content

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

Merged

Conversation

wonderingabout
Copy link
Contributor

A common trick using the spread operator :)
Credit goes to @Dorus for teaching me this trick !

@CLAassistant
Copy link

CLAassistant commented Mar 26, 2020

CLA assistant check
All committers have signed the CLA.

@wonderingabout wonderingabout force-pushed the trick-deep-copy-spread branch from 3d6fa69 to a2000fc Compare March 26, 2020 20:56
@wonderingabout wonderingabout changed the title Trick deep copy spread Trick get a new copy of object/array using spread operator Mar 26, 2020
@wonderingabout wonderingabout force-pushed the trick-deep-copy-spread branch from a2000fc to 9cc9ad2 Compare March 26, 2020 21:24
@@ -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.
````
```
Copy link
Member

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.

Copy link
Contributor Author

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()`
Copy link
Member

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?

Copy link
Contributor Author

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) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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)?

Copy link
Contributor Author

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 !
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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!

Copy link
Contributor Author

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 ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// do the arrays have the exact same contents ?
// do the arrays have the exact same contents?

Copy link
Contributor Author

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) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// are the arrays equal (they share the same reference) ?
// are the arrays equal (they share the same reference)?

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// are the arrays equal (they share the same reference) ?
// are the arrays equal (they share the same reference)?

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as #1823 (comment)

@wonderingabout wonderingabout force-pushed the trick-deep-copy-spread branch 2 times, most recently from 0c158d9 to a54f242 Compare March 27, 2020 06:51
@lex111 lex111 requested a review from paroche March 29, 2020 03:00
@paroche
Copy link
Collaborator

paroche commented Mar 29, 2020

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:'
(my preference)

Rest of the language is OK.

Anything else?

@wonderingabout wonderingabout force-pushed the trick-deep-copy-spread branch from a54f242 to f4cb058 Compare March 29, 2020 06:06
@wonderingabout
Copy link
Contributor Author

wonderingabout commented Mar 29, 2020

fixed, it's indeed clearer

i removed the unneeded "exact" word :

'Note that it is possible to do the same thing to make a copy of an object:'

@paroche
Copy link
Collaborator

paroche commented Mar 29, 2020

Fine by me!

@paroche paroche closed this Mar 29, 2020
@wonderingabout
Copy link
Contributor Author

@paroche did you accidentally hit the close PR instead of the merge PR button (just asking myself 🤔 )

@paroche
Copy link
Collaborator

paroche commented Mar 29, 2020

You could say that. Anyway, as far as I'm concerned it should be merged (and closed).

@paroche paroche reopened this Mar 29, 2020
@paroche paroche merged commit c038ef4 into javascript-tutorial:master Mar 29, 2020
@paroche
Copy link
Collaborator

paroche commented Mar 29, 2020

And it is.

Not sure if it was my place, but if not, it can be undone.

@wonderingabout
Copy link
Contributor Author

yes, you can always @ me if we need to change something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants