referencing an object's key as a variable - js
is there a way to reference an object's key as a variable and then change its value? I'm trying this
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
I'm trying to figure this out as I'm writing a recursive function in order to change root to something more like obj['x']['y']
javascript
|
show 3 more comments
is there a way to reference an object's key as a variable and then change its value? I'm trying this
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
I'm trying to figure this out as I'm writing a recursive function in order to change root to something more like obj['x']['y']
javascript
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
1
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
1
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
1
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27
|
show 3 more comments
is there a way to reference an object's key as a variable and then change its value? I'm trying this
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
I'm trying to figure this out as I'm writing a recursive function in order to change root to something more like obj['x']['y']
javascript
is there a way to reference an object's key as a variable and then change its value? I'm trying this
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
I'm trying to figure this out as I'm writing a recursive function in order to change root to something more like obj['x']['y']
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root = 'test 2';
func();
console.log(obj);
javascript
javascript
asked Nov 15 '18 at 6:20
totalnoobtotalnoob
44711034
44711034
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
1
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
1
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
1
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27
|
show 3 more comments
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
1
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
1
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
1
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
1
1
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
1
1
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
1
1
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27
|
show 3 more comments
3 Answers
3
active
oldest
votes
If I understand your question correctly, the answer is probably "not without doing really weird stuff." What I believe you are asking is this. Given:
const obj =
x:
y:
z: 'test'
you want to store obj.x
(equivalently, obj['x']
) into a variable, in such a way that assigning to that variable will actually mutate the x
field of object obj
. Now you cannot do that. Once you make the binding:
let root = obj.x
Then reassigning root
will NOT change obj
at all: root
is a distinct variable from obj
. Draw a picture, it will help. However, root
is essentially a pointer to obj.x
so if you did:
root.y = 'test 2'
than this does mutate obj.x.y
.
But, note you cannot assign obj.x
to a variable and then use that variable to mutate the x
field of obj
. You can only mutate fields WITHIN obj.x
. JavaScript does not have the ability to alias things (or make lvalues) like C++, for instance.
If you really want to update the x
property of obj
then you should put ONLY the string x
into a variable like this:
root = 'x'
then you can do:
obj[root] = 'a new value for obj.x'
and this will mutate obj.x
. But remember, you cannot first evaluate obj.x
and then use that result to mutate obj.x
. Well, I mean, not without doing some really nasty stuff like this:
root = 'obj.x';
eval(`$root = 'a new value for obj.x';`);
But don't.
By the way, if obj
was also a variable, then you could do this:
receiver = obj;
prop = 'x';
Reflect.set(receiver, prop, 'the new value')
Hopefully I guessed what you were trying to do. At any rate, maybe this answer will give some ideas.
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
add a comment |
const func = () =>
let root = obj['x'];
root = 'test 2';
This reassigns the variable root
to a new value and does not affect the object. Instead, assign to the object directly:
obj['x'] = 'test 2'
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
Or you can change the value at a key via the root
variable:
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
add a comment |
Just pass the values
const obj = x:
y:
z: 'test'
delete Object.assign(obj, newKey: obj.oldKey ).oldKey;
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53313539%2freferencing-an-objects-key-as-a-variable-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand your question correctly, the answer is probably "not without doing really weird stuff." What I believe you are asking is this. Given:
const obj =
x:
y:
z: 'test'
you want to store obj.x
(equivalently, obj['x']
) into a variable, in such a way that assigning to that variable will actually mutate the x
field of object obj
. Now you cannot do that. Once you make the binding:
let root = obj.x
Then reassigning root
will NOT change obj
at all: root
is a distinct variable from obj
. Draw a picture, it will help. However, root
is essentially a pointer to obj.x
so if you did:
root.y = 'test 2'
than this does mutate obj.x.y
.
But, note you cannot assign obj.x
to a variable and then use that variable to mutate the x
field of obj
. You can only mutate fields WITHIN obj.x
. JavaScript does not have the ability to alias things (or make lvalues) like C++, for instance.
If you really want to update the x
property of obj
then you should put ONLY the string x
into a variable like this:
root = 'x'
then you can do:
obj[root] = 'a new value for obj.x'
and this will mutate obj.x
. But remember, you cannot first evaluate obj.x
and then use that result to mutate obj.x
. Well, I mean, not without doing some really nasty stuff like this:
root = 'obj.x';
eval(`$root = 'a new value for obj.x';`);
But don't.
By the way, if obj
was also a variable, then you could do this:
receiver = obj;
prop = 'x';
Reflect.set(receiver, prop, 'the new value')
Hopefully I guessed what you were trying to do. At any rate, maybe this answer will give some ideas.
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
add a comment |
If I understand your question correctly, the answer is probably "not without doing really weird stuff." What I believe you are asking is this. Given:
const obj =
x:
y:
z: 'test'
you want to store obj.x
(equivalently, obj['x']
) into a variable, in such a way that assigning to that variable will actually mutate the x
field of object obj
. Now you cannot do that. Once you make the binding:
let root = obj.x
Then reassigning root
will NOT change obj
at all: root
is a distinct variable from obj
. Draw a picture, it will help. However, root
is essentially a pointer to obj.x
so if you did:
root.y = 'test 2'
than this does mutate obj.x.y
.
But, note you cannot assign obj.x
to a variable and then use that variable to mutate the x
field of obj
. You can only mutate fields WITHIN obj.x
. JavaScript does not have the ability to alias things (or make lvalues) like C++, for instance.
If you really want to update the x
property of obj
then you should put ONLY the string x
into a variable like this:
root = 'x'
then you can do:
obj[root] = 'a new value for obj.x'
and this will mutate obj.x
. But remember, you cannot first evaluate obj.x
and then use that result to mutate obj.x
. Well, I mean, not without doing some really nasty stuff like this:
root = 'obj.x';
eval(`$root = 'a new value for obj.x';`);
But don't.
By the way, if obj
was also a variable, then you could do this:
receiver = obj;
prop = 'x';
Reflect.set(receiver, prop, 'the new value')
Hopefully I guessed what you were trying to do. At any rate, maybe this answer will give some ideas.
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
add a comment |
If I understand your question correctly, the answer is probably "not without doing really weird stuff." What I believe you are asking is this. Given:
const obj =
x:
y:
z: 'test'
you want to store obj.x
(equivalently, obj['x']
) into a variable, in such a way that assigning to that variable will actually mutate the x
field of object obj
. Now you cannot do that. Once you make the binding:
let root = obj.x
Then reassigning root
will NOT change obj
at all: root
is a distinct variable from obj
. Draw a picture, it will help. However, root
is essentially a pointer to obj.x
so if you did:
root.y = 'test 2'
than this does mutate obj.x.y
.
But, note you cannot assign obj.x
to a variable and then use that variable to mutate the x
field of obj
. You can only mutate fields WITHIN obj.x
. JavaScript does not have the ability to alias things (or make lvalues) like C++, for instance.
If you really want to update the x
property of obj
then you should put ONLY the string x
into a variable like this:
root = 'x'
then you can do:
obj[root] = 'a new value for obj.x'
and this will mutate obj.x
. But remember, you cannot first evaluate obj.x
and then use that result to mutate obj.x
. Well, I mean, not without doing some really nasty stuff like this:
root = 'obj.x';
eval(`$root = 'a new value for obj.x';`);
But don't.
By the way, if obj
was also a variable, then you could do this:
receiver = obj;
prop = 'x';
Reflect.set(receiver, prop, 'the new value')
Hopefully I guessed what you were trying to do. At any rate, maybe this answer will give some ideas.
If I understand your question correctly, the answer is probably "not without doing really weird stuff." What I believe you are asking is this. Given:
const obj =
x:
y:
z: 'test'
you want to store obj.x
(equivalently, obj['x']
) into a variable, in such a way that assigning to that variable will actually mutate the x
field of object obj
. Now you cannot do that. Once you make the binding:
let root = obj.x
Then reassigning root
will NOT change obj
at all: root
is a distinct variable from obj
. Draw a picture, it will help. However, root
is essentially a pointer to obj.x
so if you did:
root.y = 'test 2'
than this does mutate obj.x.y
.
But, note you cannot assign obj.x
to a variable and then use that variable to mutate the x
field of obj
. You can only mutate fields WITHIN obj.x
. JavaScript does not have the ability to alias things (or make lvalues) like C++, for instance.
If you really want to update the x
property of obj
then you should put ONLY the string x
into a variable like this:
root = 'x'
then you can do:
obj[root] = 'a new value for obj.x'
and this will mutate obj.x
. But remember, you cannot first evaluate obj.x
and then use that result to mutate obj.x
. Well, I mean, not without doing some really nasty stuff like this:
root = 'obj.x';
eval(`$root = 'a new value for obj.x';`);
But don't.
By the way, if obj
was also a variable, then you could do this:
receiver = obj;
prop = 'x';
Reflect.set(receiver, prop, 'the new value')
Hopefully I guessed what you were trying to do. At any rate, maybe this answer will give some ideas.
answered Nov 15 '18 at 6:56
Ray ToalRay Toal
67.1k11125184
67.1k11125184
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
add a comment |
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
I think this is the best answer and the one I was looking for. I was confused with why stackoverflow.com/questions/53308903/… didn't work
– totalnoob
Nov 15 '18 at 7:22
add a comment |
const func = () =>
let root = obj['x'];
root = 'test 2';
This reassigns the variable root
to a new value and does not affect the object. Instead, assign to the object directly:
obj['x'] = 'test 2'
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
Or you can change the value at a key via the root
variable:
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
add a comment |
const func = () =>
let root = obj['x'];
root = 'test 2';
This reassigns the variable root
to a new value and does not affect the object. Instead, assign to the object directly:
obj['x'] = 'test 2'
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
Or you can change the value at a key via the root
variable:
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
add a comment |
const func = () =>
let root = obj['x'];
root = 'test 2';
This reassigns the variable root
to a new value and does not affect the object. Instead, assign to the object directly:
obj['x'] = 'test 2'
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
Or you can change the value at a key via the root
variable:
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
const func = () =>
let root = obj['x'];
root = 'test 2';
This reassigns the variable root
to a new value and does not affect the object. Instead, assign to the object directly:
obj['x'] = 'test 2'
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
Or you can change the value at a key via the root
variable:
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
const obj =
x:
y:
z: 'test'
const func = () =>
obj['x'] = 'test 2';
func();
console.log(obj);
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
const obj =
x:
y:
z: 'test'
const func = () =>
let root = obj['x'];
root['y'] = 'test 3';
func();
console.log(obj);
answered Nov 15 '18 at 6:24
Code-ApprenticeCode-Apprentice
48.7k1492179
48.7k1492179
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
add a comment |
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
right, but if I were creating a recursive function like in stackoverflow.com/questions/53308903/… where I needed a variable retain and to set its parent eg obj['x'] => obj['x']['y'] then that wouldn't work?
– totalnoob
Nov 15 '18 at 6:28
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
@totalnoob I don't understand what you mean by "I needed a variable retain and to set its parent". Reassigning a variable only changes the value of that variable and does not affect anything about its previous value. On the other hand, assigning to a key via a variable will affect all other references to the same object.
– Code-Apprentice
Nov 15 '18 at 6:30
1
1
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
@totalnoob These kinds of side effects are the source of many difficult-to-find bugs. I suggest you rethink your approach.
– Code-Apprentice
Nov 15 '18 at 6:33
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
I was trying to create a recursive function that iterated through an array and create nested values in an object. that function needed to know what root it's in and that root variable needed to change each time if there were multiple items in the array.
– totalnoob
Nov 15 '18 at 7:28
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
so if the array was ['x','y','z'], it'd create an object map like the one in this post, recursively and root would be obj['x'] => obj['x']['y'] => obj['x']['y']['z']
– totalnoob
Nov 15 '18 at 7:29
add a comment |
Just pass the values
const obj = x:
y:
z: 'test'
delete Object.assign(obj, newKey: obj.oldKey ).oldKey;
add a comment |
Just pass the values
const obj = x:
y:
z: 'test'
delete Object.assign(obj, newKey: obj.oldKey ).oldKey;
add a comment |
Just pass the values
const obj = x:
y:
z: 'test'
delete Object.assign(obj, newKey: obj.oldKey ).oldKey;
Just pass the values
const obj = x:
y:
z: 'test'
delete Object.assign(obj, newKey: obj.oldKey ).oldKey;
answered Nov 15 '18 at 6:42
Shubham SharmaShubham Sharma
126112
126112
add a comment |
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.
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%2f53313539%2freferencing-an-objects-key-as-a-variable-js%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
const key = 'x'; obj[key] = 'test 2';
– Robby Cornelissen
Nov 15 '18 at 6:22
1
Can you illustrate what you want to do? And more importantly show an example of what you want the result to be?
– Code-Apprentice
Nov 15 '18 at 6:22
I'm trying to figure out why this isn't working stackoverflow.com/questions/53308903/… so I simplified a part of that question as a new post
– totalnoob
Nov 15 '18 at 6:23
1
I'm thinking this question must have been asked before, but in two minutes of searching S.O. I couldn't find a duplicate. Can someone find a dupe? This seems like a super common problem with a relatively simple answer....
– Ray Toal
Nov 15 '18 at 6:26
1
@RayToal There's a canonical duplicate somewhere, but am also coming up blank.
– Robby Cornelissen
Nov 15 '18 at 6:27