0

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!

Barmar
  • 741,623
  • 53
  • 500
  • 612
WTob
  • 3
  • 3
  • Are you sure it created a new property `prop1`? It should have created a new property named `.prop`. – Barmar Sep 14 '16 at 19:47
  • [JSON](https://en.wikipedia.org/wiki/JSON), what you have there is a plain old Javascript object. – Xotic750 Sep 14 '16 at 19:56

3 Answers3

0

someOjb["1234"][prop], assuming prop is the name of that field

holtc
  • 1,780
  • 3
  • 16
  • 35
0

The property is "prop1", not .prop. You need to use square brackets when using dynamic properties:

someObj["1234"][prop] = value;
Rob M.
  • 35,491
  • 6
  • 51
  • 50
0

Like this:

someObj["1234"][prop] = value;
Sergiu
  • 1,206
  • 7
  • 9