0

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

2
  • 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. Commented Nov 20, 2018 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. Commented Nov 20, 2018 at 0:22

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.