0

JS DOESN'T have pointers - but it does have references. I have a solution to my issue which I can post if anyone is interested, but I'm wondering if there is a better way...

Is there any way in JavaScript to get a "reference" to where an object key points? For example:

let anobj = {
  a: "aval",
  b: {
    bprime: "target",
  },
  b: "bval",
  c: "cval"
};

let bptr = getReference(anobj.b.bprime);
bptr = "Updated";

// So that anobj would then be:

{
  a: "aval",
  b: {
    bprime: "Updated",
  },
  b: "bval",
  c: "cval"
};

... or something equivalent. I mean, some way to have a variable/function that represents a deeply nested "location" in an object - that can be used to assign a new value to that node of the original object.

Sorry if that's not a clear expression of my question - it's very clear in my head...

ChrisNY
  • 3,917
  • 4
  • 27
  • 31
  • 1
    Does this answer your question? [Are there pointers in javascript?](https://stackoverflow.com/questions/17382427/are-there-pointers-in-javascript) – Rojo Sep 23 '21 at 17:45
  • Also see https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-and-arrays-by-string-path – James Sep 23 '21 at 17:47
  • I found a solution to my issue - I'm suggesting this should be re-opened, not for my needs - but because I've gotten so much help from Stack Overflow I would like to contribute back. My original question mentioned "pointers" as an analogy, but clearly specified "references" - which JS DOES have. I have a correct solution I would like to post for others, and get feedback/discussion about possible better/more efficient approaches. I think it is of general interst. – ChrisNY Sep 24 '21 at 18:39

0 Answers0