3

I'm confused on how the spread operator is being used here. I have a basic understanding of how the spread operator works but I'm confused on what the spread operators are referring to, how it is being used, and what it is offering. The "userCanDecline" const is a boolean retrieved from an above call. Please let me know if I can provide any more information about what I've shared.

return [
 ...(userCanDecline
 ? [
 {
 label: 'Decline Draw',
 icon: 'ban',
 action: () => (el, ev) => {
 Service.decline(this.onCloseView);
 },
 },
 ]
 : [])
VLAZ
29.6k9 gold badges65 silver badges88 bronze badges
asked Nov 18, 2021 at 20:32
6
  • 6
    really no reason for it. should have just been written as return userCanDecline ? [{}] : []; Commented Nov 18, 2021 at 20:33
  • 1
    There is no spread operator, there is spread syntax. Commented Nov 18, 2021 at 20:36
  • You have a return statement that returns an array. Then the spread syntax inside of it allows you to perform a check, if userCanDecline evaluates to true, it returns another array of objects, otherwise it returns an empty array. The ? and : is called conditional (ternary) operator. Also the wrapping parenthesis around (userCanDecline.....) aren't necessary. Commented Nov 18, 2021 at 20:39
  • The spread syntax is simply making a copy of the array returned by the conditional operator. But there's no need to make a copy of a freshly created array. Commented Nov 18, 2021 at 20:40
  • I'd say that this code was written by someone who didn't really know what he was doing. It's not a good example of anything. Commented Nov 18, 2021 at 20:41

1 Answer 1

1

Spread syntax can be used when all elements from an object or array need to be included in a list of some kind.

let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];

In your question, what's happening is that, userCanDeclined is checked if its value is true, if so, return an array in which the array below is spread(its values copied into the new returned array)

 [
 { 
 label: 'Decline Draw',
 icon: 'ban',
 action: () => (el, ev) => {
 Service.decline(this.onCloseView);
 },
 }
 ]

Else, spread or copy an empty array if false

 []
answered Nov 18, 2021 at 21:04
Sign up to request clarification or add additional context in comments.

2 Comments

This answer makes more sense than the comments left on the post. I'm not saying they're wrong, but after playing around with removing the "..." and some other syntax they suggested, the file isn't returning what's expected.
Glad it made sense @MaxTaylor-Hayden

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.