WTob WTob - 2 months ago 5
Javascript Question

Assign a value to JSON with switch statement

My JSON:

var someObj= {
"1234": {
"prop1": "prop1value",
"prop2": "prop2value",
"prop3": [
"key1",
"key2"
]
}
}


I want to assign a new value to prop1.

Since I've got some more entries in that object I wanted to do it with switch statement by using a proper case number (1234 in this case).
I get to 1234 by:

function funct(id, prop, value) {
switch (id) {
case 1234:
someObj["1234"].prop = value;
break;
}

return(someObj);
}
funct(1234, prop1, "just something else")
}


So the function goes to object
1234
and.. adds a whole new property
prop1
with
"just something else"
assigned. I wanted it to assign
value
to
prop1
. I thought that stating
someObj["1234"].prop
would make it go to
someOjb["1234"]
and choose specified
.prop
(means
prop1
in object)

How to make it work?

Hope you get what I mean!

Answer

Like this:

someObj["1234"][prop] = value;
Comments