0

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!

1
  • sorry you said a user gets a and missed the word . i just run your code . do you mean you want to append the object ? Commented Nov 15, 2020 at 10:22

2 Answers 2

1

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

Comments

0

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

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.