使用 JavaScript 附加到 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3898563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:34:16  来源:igfitidea点击:

Appending to JSON object using JavaScript

javascriptjson

提问by Tony2K

I'm building the JSON object using JavaScript. How would I inset the following data to the bottom of the stack:

我正在使用 JavaScript 构建 JSON 对象。我将如何将以下数据插入堆栈底部:

"hello": { "label":"Hello", "url":"#hello" }

in to the following variable:

进入以下变量:

var ListData = {
  "main": {
    "label":"Main",
    "url":"#main"
  },
  "project": {
    "label":"Project",
    "url":"#project"
  },
  "settings": {
    "label":"Settings",
    "url":"#settings",
    "subnav":[
      {
        "label":"Privacy",
        "url":"#privacy"
      },
      {
        "label":"Security",
        "url":"#security"
      },
      {
        "label":"Advanced",
        "url":"#advanced"
      }
    ]
  }
};

So the variable looks like:

所以变量看起来像:

var ListData = {
  "main": {
    "label":"Main",
    "url":"#main"
  },
  "project": {
    "label":"Project",
    "url":"#project"
  },
  "settings": {
    "label":"Settings",
    "url":"#settings",
    "subnav":[
      {
        "label":"Privacy",
        "url":"#privacy"
      },
      {
        "label":"Security",
        "url":"#security"
      },
      {
        "label":"Advanced",
        "url":"#advanced"
      }
    ]
  },
  "hello": {
    "label":"Hello",
    "url":"#hello"
  }
};

I used the following code but it doesn't seem to work:

我使用了以下代码,但似乎不起作用:

var NewData = '"hello": { "label":"Hello", "url":"#hello" }';
ListData.push(NewData);

回答by Clay Hinson

You can insert it directly with an object literal:

您可以直接使用对象字面量插入它:

ListData.hello = { label: "Hello", url: "#hello" }; 

回答by Alex

If you are using jQuery, you can use the .extend()jQuery API like:

如果您使用的是 jQuery,则可以使用 . 扩展()jQuery API,如:

$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});

回答by Sohan

I have one more solution using underscore.js module,

我还有一个使用 underscore.js 模块的解决方案,

var _ = require("underscore");

var src = {
    "main": {
        "label": "Main",
        "url": "#main"
    },
    "project": {
        "label": "Project",
        "url": "#project"
    },
    "settings": {
        "label": "Settings",
        "url": "#settings",
        "subnav": [
            {
                "label": "Privacy",
                "url": "#privacy"
            },
            {
                "label": "Security",
                "url": "#security"
            },
            {
                "label": "Advanced",
                "url": "#advanced"
            }
        ]
    }
};

var dest = {"hello": { "label":"Hello", "url":"#hello" }};


var data = _.extend(src, dest);
console.log(JSON.stringify(data));

Required op :

所需操作:

{"main":{"label":"Main","url":"#main"},"project":{"label":"Project","url":"#project"},"settings":{"label":"Settings","url":"#settings","subnav":[{"label":"Privacy","url":"#privacy"},{"label":"Security","url":"#security"},{"label":"Advanced","url":"#advanced"}]},"hello":{"label":"Hello","url":"#hello"}}

回答by Robert Bolton

A JavaScript Object Literal is a comma-separated list of name/value pairs wrapped by a pair of curly braces.

JavaScript 对象字面量是由一对大括号包裹的名称/值对的逗号分隔列表。

To append the property name of encampmentname with a value of Valley Forgeto the bottom of the stack, simply add the property name after the JSON object with a dot syntax. Then specify the value. (See 'Append data' below)

要将值为Valley Forge营地名称的属性名称附加到堆栈底部,只需使用点语法在 JSON 对象后添加属性名称。然后指定值。(参见下文‘附加数据’)

You can also delete the appended name/value pair from the object literal. (See 'Delete data below')

您还可以从对象文字中删除附加的名称/值对。(参见‘删除下面的数据’)

// Start with some JSON
var myJson = { "name":"George Washington", "rank":"General", "serial":"102" };

// Append data
myJson.encampment = "Valley Forge";    

// Delete data
delete myJson.encampment

回答by Bobby Borszich

Keeping with you object literal statements just add another object to your ListData object.

与您保持对象文字语句一样,只需将另一个对象添加到您的 ListData 对象。

ListData.hello = { "label":"Hello", "url":"#hello" };

push is only for Javascript Arrays.

push 仅适用于 Javascript 数组。