3

let messages = {
  1: {
    id: '1',
    text: 'Hello World',
    userId: '1',
  },
  2: {
    id: '2',
    text: 'By World',
    userId: '2',
  },
};

// what does this statement do?
const {
  [1]: message,
  ...otherMessages
} = messages;

console.log("other messages: ", otherMessages);

We didn't have a variable otherMessages, so how does the rest syntax work on this variable? What does the above statement do in general, it's somewhat complicated?

Patrick Roberts
  • 40,065
  • 5
  • 74
  • 116
Henok Tesfaye
  • 4,681
  • 5
  • 23
  • 53
  • This is just normal [object destructuring where you collect the rest of the properties in `otherMessages`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring) – VLAZ Mar 09 '19 at 13:15
  • @CodeManiac, I don't think it is a duplicate. – Henok Tesfaye Mar 09 '19 at 13:23
  • 1
    @CodeManiac nice, I was looking for a canonical about destructuring and you have already made one a month (to the day!) ago. – VLAZ Mar 09 '19 at 13:24
  • @HenokTesfaye Did you check linked answer ? if you don't find related answer just let me know – Code Maniac Mar 09 '19 at 13:27
  • There is no such thing as a "spread operator" in JavaScript. Spread and rest are both syntaxes but neither of them are operators. This is an example of the rest syntax because it is collecting comma-separated entries from `messages`, it is not distributing comma-separated entries to anywhere like the spread syntax does. – Patrick Roberts Mar 09 '19 at 13:29

2 Answers2

3

This is a destructuring assignment. See MDN fore more information

On the left side of the = sign, you declare the variables to be destructured, and on the right side the variable to desctructure.

Doing this, you are declaring two variables message and otherMessages:

const { [1]: message, ...otherMessages } = messages;

and you are extracting the value of key 1 into message, and the rest of the messages object will be desctructured into otherMessages.

Since messages contains two entries with keys 1 and 2, otherMessages will be an object containing the remaining keys, which is only key 2.

jo_va
  • 11,779
  • 3
  • 20
  • 39
0

When trying to assign the variable otherMessages, the runtime checks to see where it is declared. As it goes up the scopes if it reaches the top level, in this case window, it will declare the variable and then assign to it using the destructuring syntax.

Think about this another way: if you were to do something like this:

otherMessages = [1, 2]

Without declaring otherMessages as a var before hand, wouldn’t the runtime declare the variable for you?

Radu Diță
  • 8,910
  • 1
  • 18
  • 27
  • 1
    "*Without declaring otherMessages as a var before hand, wouldn’t the runtime declare the variable for you*" well, not exactly. It would go up the scope chain if no binding is found for a variable, it would a new property called `otherMessages` on the global object and assign the value to it. – VLAZ Mar 09 '19 at 13:23