skip to Main Content

I have a master object, for example:

MasterObj = {
property1: …
method1: …
}

and multiple other objects:

obj1 = {
properties and methods…
},

obj2 = {
properties and methods…
}

I need to make each object as a property of the master object, like:

MasterObj.obj1 = obj1;
MasterObj.obj2 = obj2;

So that I can use the properties and methods of each little object in this way:

MasterObject.obj1.methodOfObj1(), etc…

and the original properties and methods of the MasterObject.

Is there any efficient way to add each object to the master, without having to do it for each object individually?

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Thank you to everyone who answered my question. But, I solved the problem by creating modules for each object and importing via

    import * as MasterObj from ...

    This way, I got the structure I wanted:

    MasterObject.obj1.propertiesAndMethods, MasterObject.obj2.propertiesAndMethods, and so one...

    Thanks!


  2. I’m not sure why you want it, but you can do it by putting all the objects in an array, then loop over the array and use dynamic property names.

    let objects = [obj1, obj2, obj3, ...];
    objects.forEach((i, obj) => MasterObj['obj' + (i+1)] = obj);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search