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?

1

7 Answers 7

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;
Sign up to request clarification or add additional context in comments.

1 Comment

but I don't konw how many obj I have,I can not define them previously
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(...);
}

Comments

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>

1 Comment

Actually this will reset the finalObject and not add to it as the original question asked.
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

Comments

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' } }

Comments

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]);

Comments

-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"}
              ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.