Javascript replace/modify the object that a variable points to?
up vote
0
down vote
favorite
I have a variable pointing to an object and would like to replace that object with another modified one. Is there any Javascript function that can do what my hypothetical "assign" function does in the example console session below?
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = getSubArrayWithString(x) // Equivalent to y = x[1][1][2] in this case
JSON.stringify(y)
>>> "[1,4,"Delete Me"]"
var newY = y.filter(item => item !== "Delete Me")
y.assign(newY) // Equivalent to x[1][1][2] = newY
JSON.stringify(x)
>>> "[[1,2,3],[4,[8,2,[1,4],4],6]]"
If I do y = newY
that just reassigns the y
variable to point at the newY
object, it doesn't modify x
.
I know I could modify y
in place using splice, but that won't work when I'm applying more complex changes to get newY
javascript object variables reference
add a comment |
up vote
0
down vote
favorite
I have a variable pointing to an object and would like to replace that object with another modified one. Is there any Javascript function that can do what my hypothetical "assign" function does in the example console session below?
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = getSubArrayWithString(x) // Equivalent to y = x[1][1][2] in this case
JSON.stringify(y)
>>> "[1,4,"Delete Me"]"
var newY = y.filter(item => item !== "Delete Me")
y.assign(newY) // Equivalent to x[1][1][2] = newY
JSON.stringify(x)
>>> "[[1,2,3],[4,[8,2,[1,4],4],6]]"
If I do y = newY
that just reassigns the y
variable to point at the newY
object, it doesn't modify x
.
I know I could modify y
in place using splice, but that won't work when I'm applying more complex changes to get newY
javascript object variables reference
You're not showing usgetSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also.Array.filter
does not mutate the original array, it returns a new array.
– Abana Clara
Nov 20 at 0:18
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a variable pointing to an object and would like to replace that object with another modified one. Is there any Javascript function that can do what my hypothetical "assign" function does in the example console session below?
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = getSubArrayWithString(x) // Equivalent to y = x[1][1][2] in this case
JSON.stringify(y)
>>> "[1,4,"Delete Me"]"
var newY = y.filter(item => item !== "Delete Me")
y.assign(newY) // Equivalent to x[1][1][2] = newY
JSON.stringify(x)
>>> "[[1,2,3],[4,[8,2,[1,4],4],6]]"
If I do y = newY
that just reassigns the y
variable to point at the newY
object, it doesn't modify x
.
I know I could modify y
in place using splice, but that won't work when I'm applying more complex changes to get newY
javascript object variables reference
I have a variable pointing to an object and would like to replace that object with another modified one. Is there any Javascript function that can do what my hypothetical "assign" function does in the example console session below?
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = getSubArrayWithString(x) // Equivalent to y = x[1][1][2] in this case
JSON.stringify(y)
>>> "[1,4,"Delete Me"]"
var newY = y.filter(item => item !== "Delete Me")
y.assign(newY) // Equivalent to x[1][1][2] = newY
JSON.stringify(x)
>>> "[[1,2,3],[4,[8,2,[1,4],4],6]]"
If I do y = newY
that just reassigns the y
variable to point at the newY
object, it doesn't modify x
.
I know I could modify y
in place using splice, but that won't work when I'm applying more complex changes to get newY
javascript object variables reference
javascript object variables reference
asked Nov 20 at 0:08
Esostack
3671315
3671315
You're not showing usgetSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also.Array.filter
does not mutate the original array, it returns a new array.
– Abana Clara
Nov 20 at 0:18
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22
add a comment |
You're not showing usgetSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also.Array.filter
does not mutate the original array, it returns a new array.
– Abana Clara
Nov 20 at 0:18
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22
You're not showing us
getSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also. Array.filter
does not mutate the original array, it returns a new array.– Abana Clara
Nov 20 at 0:18
You're not showing us
getSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also. Array.filter
does not mutate the original array, it returns a new array.– Abana Clara
Nov 20 at 0:18
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Ideally getSubArrayWithString()
would return a reference to the parent of the array you are interested in modifying (and maybe even the index you want). Then it's easy:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
If your really stuck with just the array reference, you can use splice()
to alter the array rather than overwrite it. You could even splice()
everything and reassign the new values, but this seems pretty inefficient:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Ideally getSubArrayWithString()
would return a reference to the parent of the array you are interested in modifying (and maybe even the index you want). Then it's easy:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
If your really stuck with just the array reference, you can use splice()
to alter the array rather than overwrite it. You could even splice()
everything and reassign the new values, but this seems pretty inefficient:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
add a comment |
up vote
1
down vote
accepted
Ideally getSubArrayWithString()
would return a reference to the parent of the array you are interested in modifying (and maybe even the index you want). Then it's easy:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
If your really stuck with just the array reference, you can use splice()
to alter the array rather than overwrite it. You could even splice()
everything and reassign the new values, but this seems pretty inefficient:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Ideally getSubArrayWithString()
would return a reference to the parent of the array you are interested in modifying (and maybe even the index you want). Then it's easy:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
If your really stuck with just the array reference, you can use splice()
to alter the array rather than overwrite it. You could even splice()
everything and reassign the new values, but this seems pretty inefficient:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
Ideally getSubArrayWithString()
would return a reference to the parent of the array you are interested in modifying (and maybe even the index you want). Then it's easy:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
If your really stuck with just the array reference, you can use splice()
to alter the array rather than overwrite it. You could even splice()
everything and reassign the new values, but this seems pretty inefficient:
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var [y, ind] = [x[1][1], 2] // getSubArrayWithString returns parent and index
y[ind] = y[ind].filter(item => item !== "Delete Me")
console.log(x)
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
var x = [[1,2,3], [4,[8,2,[1,4,"Delete Me"],4],6]]
var y = x[1][1][2]
var newY = y.filter(item => item !== "Delete Me")
y.splice(0,y.length) // sketchy, but works
Object.assign(y, newY)
console.log(x)
answered Nov 20 at 0:22
Mark Meyer
31.9k32651
31.9k32651
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
add a comment |
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
Hmm okay I do like your idea about using splice to alter and just replace everything. But yeah I do have access to the parent so if there's no better way I will just modify the parents. Here is my actual code: screencast.com/t/KYfDTuwdyq , screencast.com/t/ggQIATs0 I was hoping to replace the highlighted part with something like noteArray.assign(newNoteArray), but if that's not efficiently possible I can just leave it as it is in my picture with the logic on line 233 performed once initially and again in the highlighted section.
– Esostack
Nov 20 at 0:38
add a comment |
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%2f53384400%2fjavascript-replace-modify-the-object-that-a-variable-points-to%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
You're not showing us
getSubArrayWithString()
, which looks like a very important part of this algorithm and could be the issue. Also.Array.filter
does not mutate the original array, it returns a new array.– Abana Clara
Nov 20 at 0:18
Methods like filter creates a new list. You are right! You should always modify in place your list for this behaviour. You could maybe write a function which place y to x. But without refreshing the reference in x the two object will be different.
– Gotrank
Nov 20 at 0:22