Immutable.js - update a object that multiple nodes pointing to it
up vote
-1
down vote
favorite
It seems that, from the following example, that immutable.js creates 2 copies of node4 instead of using the same copy for node2 and node3`.
const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>Is there a way to force Immutable.js to use the same copy of node4 in both node2 and node3 before and after my setIn?
javascript immutable.js
add a comment |
up vote
-1
down vote
favorite
It seems that, from the following example, that immutable.js creates 2 copies of node4 instead of using the same copy for node2 and node3`.
const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>Is there a way to force Immutable.js to use the same copy of node4 in both node2 and node3 before and after my setIn?
javascript immutable.js
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
It seems that, from the following example, that immutable.js creates 2 copies of node4 instead of using the same copy for node2 and node3`.
const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>Is there a way to force Immutable.js to use the same copy of node4 in both node2 and node3 before and after my setIn?
javascript immutable.js
It seems that, from the following example, that immutable.js creates 2 copies of node4 instead of using the same copy for node2 and node3`.
const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>Is there a way to force Immutable.js to use the same copy of node4 in both node2 and node3 before and after my setIn?
const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>const node4 = {data: 4, next: };
const node3 = {data: 3, next: [node4]};
const node2 = {data: 2, next: [node4]};
const node1 = {data: 1, next: [node2,node3]};
const head1 = Immutable.fromJS(node1);
console.log(Immutable.getIn(head1,['next', 0,'next', 0])===Immutable.getIn(head1,['next', 1,'next', 0])); // false - why?
const head2 = Immutable.setIn(head1, ['next', 0,'next', 0, 'data'], 9);
console.log(Immutable.getIn(head2,['next', 0,'next', 0, 'data'])); // 9 - as expected
console.log(Immutable.getIn(head2,['next', 1,'next', 0, 'data'])); // 4 - not as expected - why?<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>javascript immutable.js
javascript immutable.js
asked Nov 8 at 21:56
Stav Alfi
3,88674078
3,88674078
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04
add a comment |
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216772%2fimmutable-js-update-a-object-that-multiple-nodes-pointing-to-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Isn't that what you'd expect it to do? It's a deep conversion, per the docs.
– jonrsharpe
Nov 8 at 22:04