Renaming a field in an object

后端 未结 1 1088
生来不讨喜
生来不讨喜 2020-12-03 19:45

If I have the following object:

JsonObj = {
    \"frames\": {
        \"cinema\": {
            \"sourceSize\": { \"w\": 256, \"h\": 200 },
            \"fra         


        
相关标签:
1条回答
  • 2020-12-03 20:26

    Set the somethingElse as a reference to what frames points to, then delete frames.

    parsedJSON.somethingElse = parsedJSON.frames;
    delete parsedJSON.frames;
    

    The important thing here is that frames is simply a pointer to an object; if you delete the frames pointer, somethingElse still references a valid object.


    Also note there's no such thing as a "JSON object"; you have a JSON representation of an object, which is a string, or you have an object (which can often be defined via object literal notation, which is often where the confusion lies).

    0 讨论(0)
提交回复
热议问题