Javascript 如何将javascript对象动态添加到另一个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10118204/
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 23:55:11  来源:igfitidea点击:

How can I add javascript object to another object in dynamic

javascriptobjectadd

提问by hh54188

Suppose I have several javascript object

假设我有几个 javascript 对象

{"type":"gotopage","target":"undefined"}
{"type":"press","target":"a"}
{"type":"rotate","target":"long"}

How can I add this objects to another object like

如何将此对象添加到另一个对象,例如

config={}

I know how to if each inserted object have a id I can added as:

我知道如果每个插入的对象都有一个 id 我可以添加为:

config["id"]={}

But in this case how can I add objects without id?

但在这种情况下,如何添加没有 id 的对象?

采纳答案by ThiefMaster

I think you are mixing up objects and arrays. Objects have named keys, arrays have numeric keys. You can easily append to an array using .push():

我认为您正在混淆对象和数组。对象有命名键,数组有数字键。您可以使用.push()以下方法轻松附加到数组:

var arr = [];
arr.push("something");

However, for a configuration object as your variable name suggests this is not really useful. You should rather use meaningful names for your options, e.g.:

但是,对于变量名称所暗示的配置对象,这并不是很有用。您应该为您的选项使用有意义的名称,例如:

var config = {
    something: 'blah',
    foo: 'bar'
};

You can also store an array in your object:

您还可以在对象中存储数组:

config.manyStuff = [];
for(...) {
    manyStuff.push(...);
}

回答by Boyo

var obj1 = {"type":"gotopage","target":"undefined"};

var config = {};
config.obj1 = obj1;

You can always add them later.

您可以随时添加它们。

var obj1 = {"type":"gotopage","target":"undefined"};

var config = {};
config.obj1 = obj1;

var obj2 = {"key":"data"};
config.obj2 = obj2;

回答by Joseph

If these data you have are of the same type, then use an array instead:

如果您拥有的这些数据属于同一类型,请改用数组:

var some_data = {"type":"gotopage","target":"undefined"}

var config = [];
var config.push(some_data); //and do this for the rest

And you can access the config items like this:

您可以像这样访问配置项:

var someVariable = config[index]; //where index is a number starting from 0

回答by Rondo

I'm using this utility function:

我正在使用此实用程序功能:

module.exports.combine = function() {

  var rv = {};
  for (i = 0; i < arguments.length; i++) {
    for (thing in arguments[i]) {
        rv[thing]=arguments[i][thing];
    }
  }
  return rv;
}

Properties with the same name will be overwritten by properties in later arguments

具有相同名称的属性将被后面参数中的属性覆盖

var util = require('lib/util.js');
console.log(util.combine(
  {"1":"one","two":function(){;},
  {"four":{"four.5":"four.5-b"}, "5":"foo"}});

result:

结果:

    { '1': 'one',
    '5': 'foo',
    two: [Function],
    four: { 'four.5': 'four.5-b' } }

回答by user2713949

If you want to add object you can easily add using use spread operator.

如果要添加对象,可以使用扩展运算符轻松添加。

Example ->

示例 ->

object1 = { property1: 1, property2: 2}
finalObject = [{...object1}]

finalObject = {...object1}

finalObject = {...object1}

and as per your question solution use array object here

并根据您的问题解决方案在此处使用数组对象

finalObject = [{...object1}]

finalObject = [{...object1}]

object1 = { property1: 1, property2: 2}
    finalObject = [{...object1}]
    document.getElementById("demo").innerHTML = JSON.stringify(finalObject);
<div id = "demo"></div>

回答by Krishna

If you are looking to add them during intialization, this might help you out:

如果您希望在初始化期间添加它们,这可能会对您有所帮助:

 var config = [
               {"type":"gotopage","target":"undefined"},
               {"type":"press","target":"a"},
               {"type":"rotate","target":"long"}
              ]