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

3
  • 1
    Does this answer your question? Are there pointers in javascript? Commented Sep 23, 2021 at 17:45
  • Also see stackoverflow.com/questions/6491463/… Commented Sep 23, 2021 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. Commented Sep 24, 2021 at 18:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.