Does both assignment considered as Spread?

Viewed 50

I have the following object:

const restaurant = {
  openingHours: {
    thu: {
      open: 12,
      close: 22,
    },
    fri: {
      open: 11,
      close: 23,
    },
    saturday: {
      open: 0, // Open 24 hours
      close: 24,
    },
  }
};

and two pieces of code that achieve the same result but are written a little bit differently:

const { fri } = restaurant.openingHours;
const friday = { ...fri };

this above is Spread syntax for sure

const { fri: {...friday} } = restaurant.openingHours;

Is the second one is a Rest syntax? I'm not sure what is the order of the assignment (that includes also destructing) in the code

1 Answers
const { fri: {...friday} } = restaurant.openingHours;

Here ...friday is a Rest syntax because we are creating an object from an indefinite number of values.

According to DigitalOcean documentaion

  • Spread syntax is used to unpack iterables such as arrays, objects, and function calls.
  • Rest parameter syntax will create an array from an indefinite number of values.
Related