3

How would I pass Object.assign dynamically?

Suppose this is my object:

this.driverForm.value.drivers

0:{last_name: "Are", first_name: "", gender: "", dob: "", address: "", …}

Now I can easily assign objects like this:

 const submittedForm = Object.assign(
        {}, this.driverForm.value.drivers[0],
 );

But how would I assign objects if my values changes like this:

0:{last_name: "Are", first_name: "", gender: "", dob: "", address: "", …}

1:{last_name: "Wert", first_name: "", gender: "", dob: "", address: "", …}

Here is the full code

saveNewDriver() {
    console.log(this.driverForm.value.drivers,'driver save');
    const submittedForm = Object.assign(
        {}, this.driverForm.value.drivers[0], {enable: true},
        {checked: false},
    );
    this.store.select('drivers').dispatch(new createDriver(submittedForm));
}
3
  • 2
    I'm sorry but I don't thing your question is clear. Would you like to add another property to the object or is your object actually an array? Commented May 17, 2018 at 10:27
  • Yes I mean is an array, Commented May 17, 2018 at 10:30
  • What end result do you want? If you used Object.assign with both of those objects, you'd only get one set of their values. Commented May 17, 2018 at 10:50

3 Answers 3

1

I don't know if I did the right thing, but I make it work

 saveNewDriver() {
    const myarray = this.driverForm.value.drivers;
    const submittedForm = myarray.map(a => ({...a, enable: true, checked: false}));
    submittedForm.forEach(driver => {
        this.store.select('drivers').dispatch(new createDriver(driver));
    });
}

Please let me know more elegant solution

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct solution, I've found the same solution, using Object.assign, because it was asked in your question.
I applied your solution, because that is what I really wanted
0

Since you should avoid mixing number-based properties, like '0' from an array, and named properties, like 'enable', i strongly recommend you to create an array, based on each driver object:

saveNewDriver() {
    console.log(this.driverForm.value.drivers,'driver save');
    const submittedForms = this.driverForm.value.drivers.map(driver => 
            Object.assign({},
                driver,
                {enable: true},
                {checked: false}
            );
    );
    submittedForms.forEach(form => {
        this.store.select('drivers').dispatch(new createDriver(form));
    });
}

1 Comment

I would not like to mutate the driver form , because, i have to add another object in Object.assign something like this const submittedForm = Object.assign( {}, this.driverForm.value.drivers, {enable: true}, {checked: false}, );
0

I assume you don't want to mutate this.driverForm and add a new item to this.driverForm.value.drivers

this.driverForm = Object.assign(
  {},
  this.driverForm,
  {value:Object.assign(
    {},
    this.driverForm.value,
    {drivers:this.driverForm.value.drivers.concat(newDriver)}
  )}
)

With spread operator it'll look like this:

this.driverForm = {
  ...this.driverForm,
  value:{
    ...this.driverForm.value,
    drivers:this.driverForm.value.driverForm.concat(newDriver)
  }
}

Comments

Your Answer

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