Allegium
Allegium

Reputation: 11

How could I create new objects in an object with a function?

I need to create new objects in an object with a function. It should work this way: I need to collect some data into a big object that consists of some smaller objects that I should create using a function. It works this way: a user gets a created by my function object, fills it and then this object is pushed to the big object. The problem is I don't totally understand how to create a new object every time because my code just changes data in one object. The creation of the new function should happens only when it's required by a user.

My code:

let ger_tasks = {}
let name = 1;
let descr = 2;
let timing = 3;


function newTask(name,descr,timing) {
    this.name = name;
     this.descr = descr;
     this.timing = timing;
        name = {
        description:descr,
        timings:timing,
       
    }
    ger_tasks.name = name;
    }
    newTask('a','b','c')
    newTask('0','1','2')
    console.log(ger_tasks)

Thanks in advance!

Upvotes: 0

Views: 53

Answers (2)

tawfikboujeh
tawfikboujeh

Reputation: 107

Here is a snippet of my try on it :

let ger_task = {}
let name = 1;
let descr = 2;
let timing = 3;


function newTask(name,descr,timing) {
    this.name = name;
    this.descr = descr;
    this.timing = timing;
    content = {
        description:this.descr,
        timings:this.timing,
       
    }
    ger_task[name] = this.content;
    }
    newTask('a','b','c')
    newTask('0','1','2')
    console.log(ger_task)

Upvotes: 1

user14520680
user14520680

Reputation:

Use square bracket notation:

ger_tasks[name] = /*varname*/;

Also, don't call your variable name - instead something else so that the parameter is not overwritten

Upvotes: 0

Related Questions