简体   繁体   中英

How to update value in JSON object using NodeJS

Here is how my json looks:

{
  Name:
    {
      "a": 2,
      "b": "hello"
    }
}

Here is how my nodejs file looks:

jsonfilename[Name].a = 12;
jsonfilename[Name].b = "bye";

Im not sure why it's not updating it! Thanks for any help.

Only use square brackets when you are trying to use an expression to access a property. If you know the name of the property just use dot notation :

jsonfilename.Name.a = 12;
jsonfilename.Name.b = "bye";

If you use bracket notation then you must include an expression that evaluates to a string or Symbol. In your case you want the string "Name":

jsonfilename["Name"].a = 12;
jsonfilename["Name"].b = "bye";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM