19

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,887
  • 32
  • 113
  • 184
  • 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

7 Answers7

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
  • 310,957
  • 84
  • 592
  • 636
5

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>
user2713949
  • 51
  • 1
  • 2
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
  • 10,259
  • 10
  • 67
  • 98
Joseph
  • 117,725
  • 30
  • 181
  • 234
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,458
  • 28
  • 26
0

To add/append one object content to another such as,

let obj = { id:"hello", class: "world"};
let extra = {style:"{color:red}"};

obj = {...obj, ...extra}; //extra values will be appended to obj.

However, note that if you original object is declared as a const then it cannot be reassigned, and therefore the following solution will work,

const obj = {id:"hello", class: "world"};
let extra = {style:"{color:red}"};

Object.keys(extra).forEach(k=> obj[k] = extra[k]);
Aurovrata
  • 2,000
  • 27
  • 45
-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
  • 316
  • 2
  • 14
Krishna
  • 636
  • 3
  • 8