最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Conditionally pushing to an array with spread operator - Stack Overflow

matteradmin3PV0评论

I am pushing elements to an array based on a condition as explained here .html

const arr = [
  ...(cond ? ['a'] : []),
  'b',
];

Now, this works fine, but when I try

const arr = [
  ...(cond && ['a']),
  'b',
];

instead, it stops working.

I would like to know why it's not working anymore, and if there is a way to conditionally push using spread operator and && instead of ?.

Thank you

I am pushing elements to an array based on a condition as explained here http://2ality./2017/04/conditional-literal-entries.html

const arr = [
  ...(cond ? ['a'] : []),
  'b',
];

Now, this works fine, but when I try

const arr = [
  ...(cond && ['a']),
  'b',
];

instead, it stops working.

I would like to know why it's not working anymore, and if there is a way to conditionally push using spread operator and && instead of ?.

Thank you

Share Improve this question asked Feb 14, 2018 at 9:02 user3808307user3808307 1,47312 gold badges63 silver badges114 bronze badges 6
  • 1 please add the value of cond. – Nina Scholz Commented Feb 14, 2018 at 9:03
  • In the second example, if cond evaluates to false, the expression evaluates to false and you end up with ...false instead of ...[] therefore throwing an error. – Miguel Calderón Commented Feb 14, 2018 at 9:10
  • @NinaScholz cond is a condition, and as such, may be true or false – user3808307 Commented Feb 14, 2018 at 9:13
  • @Miguel in the case I am using to test particulary it evaluates to true, and it does not add "a" to the array. Either way, do you know to fix this? – user3808307 Commented Feb 14, 2018 at 9:15
  • 1 @user3808307 you can try in the console: [...(true && ['a'])] gives ['a'] but [...(false && ['a'])] yield a TypeError. – Seb D. Commented Feb 14, 2018 at 9:41
 |  Show 1 more ment

2 Answers 2

Reset to default 7

No, it is not possible, because all iterable objects are truthy.

If cond is falsey, you have a value which is not spreadable by Symbol.iterator

The built-in types with a @@iterator method are:

  • Array.prototype[@@iterator]()
  • TypedArray.prototype[@@iterator]()
  • String.prototype[@@iterator]()
  • Map.prototype[@@iterator]()
  • Set.prototype[@@iterator]()

var cond = false;

const arr = [
  ...(cond && ['a']),  // throws error, function expected
  'b',
];

console.log(arr);

Yes, it's possible. But maybe that's an overkill that performs worse and decreases the readability.

const arr = [];
arr.push(...[false && 'nope'].filter(v => v));
arr.push(...[true && 'yep'].filter(v => v));
arr.push(...[false && 'no', true && 'yes'].filter(v => v));
    
console.info(arr);

As @Nina Scholz indicated an iterable is required for spread operator to work. By using a second array (which may be empty) we can eventually reach the following state (arr.push()).

Post a comment

comment list (0)

  1. No comments so far