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










share|improve this question






















  • 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















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










share|improve this question






















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 0:08









Esostack

3671315




3671315












  • 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


















  • 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
















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












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)








share|improve this answer





















  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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)








share|improve this answer





















  • 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

















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)








share|improve this answer





















  • 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















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)








share|improve this answer












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)






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen