-1

I'm adding data to the formData by appending each item from my javascript object and it looks like this:

  let formData = new FormData();
  formData.append('File', document.file);


  product = {
    ...values,
    productType: productT,
  };

  const DataTransformer = {
   createdDate: howtoreadvaluefrom...values, // How can I read value createdDate from ...values
   expirationDate: howtoreadvaluefrom...values, // How can I read value expirationDate from ...values
  };


  for (var key in product) {
    if (key != 'createdDate' && key != 'expirationDate') {
      formData.append(key, product[key]);
    } else {
      formData.append(key, new Date(product[key]).toUTCString());
    }
  }

  await createproduct(formData);

As you can see there is DataTransformer variable and I would like to populate those two props from ...values but I'm not sure what's proper way to do it..

Thanks a lot

Cheers

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • 3
    What is `values`? Do you want `values.createdDate`? – CertainPerformance Nov 18 '19 at 08:27
  • @CertainPerformance Yeah, values containts `createdDate` and `expirationDate`, and I would like to read them from `...values`. – Roxy'Pro Nov 18 '19 at 08:29
  • @Roxy'Pro `...values` is not a tangible value, it's not an expression. It's part of an object literal, it's a syntax error on its own. That's like asking how to get the property `createdDate` from `:`. – Bergi Nov 18 '19 at 09:25

1 Answers1

0

You can do it like this:-

// assuming values has these two properties
const {  createdDate, createdDate } = values;
const DataTransformer = {
    createdDate,
    createdDate
}

Hope this helps!!

Shivratna Kumar
  • 1,311
  • 1
  • 8
  • 18