0

I am trying to push new data in a secondary array using javascript in React but I am unable to do it.

I have my object declared as this:

 const {
    id,
    user,
    task
  } = documentTask;

  const {
    time,
    duration,
    sum
  } = task;

And then I am trying to push a value to the task field from another class.

  tasksWithOverallValues.push({
    duration: overallSeconds,
    sum: overallPrice
  });

tasksWithOverallValues is a list of documentTask's.

When I am doing what is shown .push({duration... then the object becomes undefined. My idea was to call :

  tasksWithOverallValues.push({
    task.duration: overallSeconds,
    task.sum: overallPrice
  });

Because my it is trying to push the values into an unkown list, but I get the following error:

'task' is not defined  no-undef

After that is done the object becomes undefined and I cannot do anything with it.

So my question is, how can I push new values into a secondary list?

2
  • 1
    What you mean by "I cannot call task.duration/sum"? Can you share an example of what you are trying to do and what error you got? Commented Apr 3, 2020 at 12:57
  • 1
    Can you create a small demo for this using jsfiddle or snippet here to better understand your issue. You don't need to use large array of objects. use some simple object to reproduce the issue in demo. Commented Apr 3, 2020 at 13:06

1 Answer 1

1

You said tasksWithOverallValues is a list of documentTasks. So if you're pushing something in there, it should be a documentTask, like:

tasksWithOverallValues.push({
    id: someId,
    user: someUser,
    task: {
        duration: overallSeconds,
        sum: overallPrice
    }
});

Or if you already have or create the task beforehand:

newTask = {
    duration: overallSeconds,
    sum: overallPrice
};
// ...
tasksWithOverallValues.push({
    id: someId,
    user: someUser,
    task: newTask
});
Sign up to request clarification or add additional context in comments.

6 Comments

This seems to be exactly what I was looking for, thank you,
One more question. If task has 3 variables duration, sum, time. Is there a way to keep the third variable as null so it would not show? In my case the duration and sum are changing as they should but third variable under task now also tries to change and it gives me unwanted results.
If I understand correctly, you want to change duration and sum for a task with a specific id in the array, is that right? In that case, you don't want to push an element, you want to modify an existing one. I'll edit the answer in a minute.
no what you answered was correct. I want to push it as the last element so it would show the total in the excel that I will export. But the task has a third value and since I am pushing the other 2 values of the task the third value which is time is also shown in excel on the last row.
Well, you could add time: null in the task you're pushing, like task: { duration: overallSeconds, sum: overallPrice, time: null }, but it's not very clear to me how you are using these objects, and what is going on with the export.
|

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.