15

Suppose I have several javascript object

{"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:

config["id"]={}

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

hh54188
  • 14,027
  • 31
  • 107
  • 178
  • Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – Gabe Gates Aug 14 '18 at 19:21

6 Answers6

16
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;
Boyo
  • 1,381
  • 12
  • 20
9

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():

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(...);
}
ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
2

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
A.L
  • 9,606
  • 9
  • 60
  • 93
Joseph
  • 113,089
  • 28
  • 177
  • 225
2

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

Example ->

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

finalObject = {...object1}

and as per your question solution use array object here

finalObject = [{...object1}]

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

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' } }
Rondo
  • 3,120
  • 26
  • 26
-2

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"}
              ]
Kaleem Elahi
  • 176
  • 1
  • 12
Krishna
  • 616
  • 3
  • 8