重命名对象中的字段

重命名对象中的字段

本文介绍了重命名对象中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下物品:

JsonObj = {
    "frames": {
        "cinema": {
            "sourceSize": { "w": 256, "h": 200 },
            "frame": { "x": 0, "y": 0, "w": 256, "h": 192 }
        },
        "tree": {
            "sourceSize": { "w": 128, "h": 110 },
            "frame": { "x": 0, "y": 302, "w": 70, "h": 96 }
        }
    }
};

此JSON对象被解析为变量 parsedJSON 使用此JavaScript代码:

This JSON object is parsed into the variable parsedJSON using this JavaScript code:

var parsedJSON = JSON.parse(JsonObj);

如何重命名 parsedJSON中的frames属性到别的什么地方?

How would I rename the "frames" property in parsedJSON to something else?

推荐答案

somethingElse 设置为参考什么 frames 指向,然后删除 frames

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

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

这里重要的是 frames 是只是一个指向对象的指针;如果删除 frames 指针, somethingElse 仍引用有效对象。

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.

这篇关于重命名对象中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:29