Add elements inside Array conditionally in JavaScriptIn Javascript, how to conditionally add a member to an object?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
On what legal basis did the UK remove the 'European Union' from its passport?
LocalDate.plus Incorrect Answer
What is the best way for a skeleton to impersonate human without using magic?
Was there a contingency plan in place if Little Boy failed to detonate?
Why does TypeScript pack a Class in an IIFE?
Why did God specifically target the firstborn in the 10th plague (Exodus 12:29-36)?
How old is Captain America at the end of "Avengers: Endgame"?
Make all the squares explode
Guns in space with bullets that return?
Can you book a one-way ticket to the UK on a visa?
A musical commute to work
Why was this sacrifice sufficient?
Is it a Munchausen Number?
Is there enough time to Planar Bind a creature conjured by a 1-hour-duration spell?
Understanding basic photoresistor circuit
Is Simic Ascendancy triggered by Awakening of Vitu-Ghazi?
How to make a language evolve quickly?
Pre-1993 comic in which Wolverine's claws were turned to rubber?
Why does a C.D.F need to be right-continuous?
How do I get past a 3-year ban from overstay with VWP?
Is the homebrew weapon attack cantrip 'Arcane Strike' balanced?
Control variables and other independent variables
Set a camera to free fall like a Rigid Body?
Ex-manager wants to stay in touch, I don't want to
Add elements inside Array conditionally in JavaScript
In Javascript, how to conditionally add a member to an object?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When I try to merge two objects using the spread operator conditionally, it works when the condition is true
or false
:
let condition = false;
let obj1 = key1: 'value1'
let obj2 =
key2: 'value2',
...(condition && obj1),
;
// obj2 = key2: 'value2';
When I try to use the same logic with Arrays, it only works when the condition is true
:
let condition = true;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// arr2 = ['value2', 'value1']
If the condition is false
an error is thrown:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
Why is the behaviour different between Array
and Object
?
javascript ecmascript-6
add a comment |
When I try to merge two objects using the spread operator conditionally, it works when the condition is true
or false
:
let condition = false;
let obj1 = key1: 'value1'
let obj2 =
key2: 'value2',
...(condition && obj1),
;
// obj2 = key2: 'value2';
When I try to use the same logic with Arrays, it only works when the condition is true
:
let condition = true;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// arr2 = ['value2', 'value1']
If the condition is false
an error is thrown:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
Why is the behaviour different between Array
and Object
?
javascript ecmascript-6
Not cleared. What do you want to achieve here? Can you please paste your(condition && arr)
as well?
– narayansharma91
May 7 at 11:34
FYI,...null
and...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not
– adiga
May 7 at 12:13
1
FWIW if you wanted to spread booleans, you could do something likeBoolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)
– Conor O'Brien
May 7 at 14:00
Is what you really want to achievecondition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?
– JollyJoker
May 7 at 14:20
2
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34
add a comment |
When I try to merge two objects using the spread operator conditionally, it works when the condition is true
or false
:
let condition = false;
let obj1 = key1: 'value1'
let obj2 =
key2: 'value2',
...(condition && obj1),
;
// obj2 = key2: 'value2';
When I try to use the same logic with Arrays, it only works when the condition is true
:
let condition = true;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// arr2 = ['value2', 'value1']
If the condition is false
an error is thrown:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
Why is the behaviour different between Array
and Object
?
javascript ecmascript-6
When I try to merge two objects using the spread operator conditionally, it works when the condition is true
or false
:
let condition = false;
let obj1 = key1: 'value1'
let obj2 =
key2: 'value2',
...(condition && obj1),
;
// obj2 = key2: 'value2';
When I try to use the same logic with Arrays, it only works when the condition is true
:
let condition = true;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// arr2 = ['value2', 'value1']
If the condition is false
an error is thrown:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
Why is the behaviour different between Array
and Object
?
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr1)];
// Error
javascript ecmascript-6
javascript ecmascript-6
edited May 8 at 0:00
CertainPerformance
105k166798
105k166798
asked May 7 at 11:31
João RodriguesJoão Rodrigues
288211
288211
Not cleared. What do you want to achieve here? Can you please paste your(condition && arr)
as well?
– narayansharma91
May 7 at 11:34
FYI,...null
and...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not
– adiga
May 7 at 12:13
1
FWIW if you wanted to spread booleans, you could do something likeBoolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)
– Conor O'Brien
May 7 at 14:00
Is what you really want to achievecondition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?
– JollyJoker
May 7 at 14:20
2
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34
add a comment |
Not cleared. What do you want to achieve here? Can you please paste your(condition && arr)
as well?
– narayansharma91
May 7 at 11:34
FYI,...null
and...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not
– adiga
May 7 at 12:13
1
FWIW if you wanted to spread booleans, you could do something likeBoolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)
– Conor O'Brien
May 7 at 14:00
Is what you really want to achievecondition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?
– JollyJoker
May 7 at 14:20
2
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34
Not cleared. What do you want to achieve here? Can you please paste your
(condition && arr)
as well?– narayansharma91
May 7 at 11:34
Not cleared. What do you want to achieve here? Can you please paste your
(condition && arr)
as well?– narayansharma91
May 7 at 11:34
FYI,
...null
and ...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not– adiga
May 7 at 12:13
FYI,
...null
and ...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not– adiga
May 7 at 12:13
1
1
FWIW if you wanted to spread booleans, you could do something like
Boolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)– Conor O'Brien
May 7 at 14:00
FWIW if you wanted to spread booleans, you could do something like
Boolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)– Conor O'Brien
May 7 at 14:00
Is what you really want to achieve
condition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?– JollyJoker
May 7 at 14:20
Is what you really want to achieve
condition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?– JollyJoker
May 7 at 14:20
2
2
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34
add a comment |
3 Answers
3
active
oldest
votes
When you spread into an array, you call the Symbol.iterator
method on the object. &&
evaluates to the first falsey value (or the last truthy value, if all are truthy), so
let arr2 = ['value2', ...(condition && arr)];
results in
let arr2 = ['value2', ...(false)];
But false
does not have a Symbol.iterator
method.
You could use the conditional operator instead, and spread an empty array if the condition is false:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
(This works because the empty array does have the Symbol.iterator
method)
Object spread is completely different: it copies own enumerable properties from a provided object onto a new object. false
does not have any own enumerable properties, so nothing gets copied.
add a comment |
false
is not spreadable.
You need a spreadable object (the one where Symbol.iterator
is implemented) which returns nothing, if spreaded.
You could use an empty array as default value. This works even if arr
is falsy.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
would add parenthesis to make it clearer((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
This could really use the?:
operator instead of a chained||
.
– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
I think this is a use case where?:
is a lot more appropriate. ;)
– deceze♦
May 7 at 11:39
add a comment |
This is a specification difference between the spread syntax for object literals and for array literals.
MDN briefly mentions it here -- I highlight:
Spread syntax (other than in the case of spread properties) can be applied only to iterable objects
The difference comes from the EcmaScript 2018 specification:
Concerning object spread syntax, see 12.2.6.8 Runtime Semantics: PropertyDefinitionEvaluation:
- It calls
CopyDataProperties(object, fromValue, excludedNames)
where the fromValue is wrapped to an object withToObject
, and therefore becomes iterable, even if fromValue is a primitive value likefalse
. Therefore...false
is valid EcmaScript.
- It calls
Concerning array spread syntax, see 12.2.5.2 Runtime Semantics: ArrayAccumulation:
- It merely calls
GetValue(spreadRef)
which does not do the above mentioned wrapping. And so the subsequent call toGetIterator
will trigger an error on a primitive value, as it is not iterable. Therefore[...false]
is invalid EcmaScript.
- It merely calls
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56021631%2fadd-elements-inside-array-conditionally-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you spread into an array, you call the Symbol.iterator
method on the object. &&
evaluates to the first falsey value (or the last truthy value, if all are truthy), so
let arr2 = ['value2', ...(condition && arr)];
results in
let arr2 = ['value2', ...(false)];
But false
does not have a Symbol.iterator
method.
You could use the conditional operator instead, and spread an empty array if the condition is false:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
(This works because the empty array does have the Symbol.iterator
method)
Object spread is completely different: it copies own enumerable properties from a provided object onto a new object. false
does not have any own enumerable properties, so nothing gets copied.
add a comment |
When you spread into an array, you call the Symbol.iterator
method on the object. &&
evaluates to the first falsey value (or the last truthy value, if all are truthy), so
let arr2 = ['value2', ...(condition && arr)];
results in
let arr2 = ['value2', ...(false)];
But false
does not have a Symbol.iterator
method.
You could use the conditional operator instead, and spread an empty array if the condition is false:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
(This works because the empty array does have the Symbol.iterator
method)
Object spread is completely different: it copies own enumerable properties from a provided object onto a new object. false
does not have any own enumerable properties, so nothing gets copied.
add a comment |
When you spread into an array, you call the Symbol.iterator
method on the object. &&
evaluates to the first falsey value (or the last truthy value, if all are truthy), so
let arr2 = ['value2', ...(condition && arr)];
results in
let arr2 = ['value2', ...(false)];
But false
does not have a Symbol.iterator
method.
You could use the conditional operator instead, and spread an empty array if the condition is false:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
(This works because the empty array does have the Symbol.iterator
method)
Object spread is completely different: it copies own enumerable properties from a provided object onto a new object. false
does not have any own enumerable properties, so nothing gets copied.
When you spread into an array, you call the Symbol.iterator
method on the object. &&
evaluates to the first falsey value (or the last truthy value, if all are truthy), so
let arr2 = ['value2', ...(condition && arr)];
results in
let arr2 = ['value2', ...(false)];
But false
does not have a Symbol.iterator
method.
You could use the conditional operator instead, and spread an empty array if the condition is false:
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
(This works because the empty array does have the Symbol.iterator
method)
Object spread is completely different: it copies own enumerable properties from a provided object onto a new object. false
does not have any own enumerable properties, so nothing gets copied.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition ? arr1 : [])];
console.log(arr2);
edited May 7 at 14:30
Nzall
3,03331950
3,03331950
answered May 7 at 11:34
CertainPerformanceCertainPerformance
105k166798
105k166798
add a comment |
add a comment |
false
is not spreadable.
You need a spreadable object (the one where Symbol.iterator
is implemented) which returns nothing, if spreaded.
You could use an empty array as default value. This works even if arr
is falsy.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
would add parenthesis to make it clearer((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
This could really use the?:
operator instead of a chained||
.
– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
I think this is a use case where?:
is a lot more appropriate. ;)
– deceze♦
May 7 at 11:39
add a comment |
false
is not spreadable.
You need a spreadable object (the one where Symbol.iterator
is implemented) which returns nothing, if spreaded.
You could use an empty array as default value. This works even if arr
is falsy.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
would add parenthesis to make it clearer((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
This could really use the?:
operator instead of a chained||
.
– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
I think this is a use case where?:
is a lot more appropriate. ;)
– deceze♦
May 7 at 11:39
add a comment |
false
is not spreadable.
You need a spreadable object (the one where Symbol.iterator
is implemented) which returns nothing, if spreaded.
You could use an empty array as default value. This works even if arr
is falsy.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
false
is not spreadable.
You need a spreadable object (the one where Symbol.iterator
is implemented) which returns nothing, if spreaded.
You could use an empty array as default value. This works even if arr
is falsy.
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
let condition = false;
let arr1 = ['value1'];
let arr2 = ['value2', ...(condition && arr || [])];
console.log(arr2);
edited May 7 at 11:42
answered May 7 at 11:36
Nina ScholzNina Scholz
204k16117187
204k16117187
would add parenthesis to make it clearer((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
This could really use the?:
operator instead of a chained||
.
– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
I think this is a use case where?:
is a lot more appropriate. ;)
– deceze♦
May 7 at 11:39
add a comment |
would add parenthesis to make it clearer((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
This could really use the?:
operator instead of a chained||
.
– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
I think this is a use case where?:
is a lot more appropriate. ;)
– deceze♦
May 7 at 11:39
would add parenthesis to make it clearer
((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
would add parenthesis to make it clearer
((condition && arr) || [])
– Grégory NEUT
May 7 at 11:37
5
5
This could really use the
?:
operator instead of a chained ||
.– deceze♦
May 7 at 11:37
This could really use the
?:
operator instead of a chained ||
.– deceze♦
May 7 at 11:37
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
@deceze, it really depends on the use case.
– Nina Scholz
May 7 at 11:39
6
6
I think this is a use case where
?:
is a lot more appropriate. ;)– deceze♦
May 7 at 11:39
I think this is a use case where
?:
is a lot more appropriate. ;)– deceze♦
May 7 at 11:39
add a comment |
This is a specification difference between the spread syntax for object literals and for array literals.
MDN briefly mentions it here -- I highlight:
Spread syntax (other than in the case of spread properties) can be applied only to iterable objects
The difference comes from the EcmaScript 2018 specification:
Concerning object spread syntax, see 12.2.6.8 Runtime Semantics: PropertyDefinitionEvaluation:
- It calls
CopyDataProperties(object, fromValue, excludedNames)
where the fromValue is wrapped to an object withToObject
, and therefore becomes iterable, even if fromValue is a primitive value likefalse
. Therefore...false
is valid EcmaScript.
- It calls
Concerning array spread syntax, see 12.2.5.2 Runtime Semantics: ArrayAccumulation:
- It merely calls
GetValue(spreadRef)
which does not do the above mentioned wrapping. And so the subsequent call toGetIterator
will trigger an error on a primitive value, as it is not iterable. Therefore[...false]
is invalid EcmaScript.
- It merely calls
add a comment |
This is a specification difference between the spread syntax for object literals and for array literals.
MDN briefly mentions it here -- I highlight:
Spread syntax (other than in the case of spread properties) can be applied only to iterable objects
The difference comes from the EcmaScript 2018 specification:
Concerning object spread syntax, see 12.2.6.8 Runtime Semantics: PropertyDefinitionEvaluation:
- It calls
CopyDataProperties(object, fromValue, excludedNames)
where the fromValue is wrapped to an object withToObject
, and therefore becomes iterable, even if fromValue is a primitive value likefalse
. Therefore...false
is valid EcmaScript.
- It calls
Concerning array spread syntax, see 12.2.5.2 Runtime Semantics: ArrayAccumulation:
- It merely calls
GetValue(spreadRef)
which does not do the above mentioned wrapping. And so the subsequent call toGetIterator
will trigger an error on a primitive value, as it is not iterable. Therefore[...false]
is invalid EcmaScript.
- It merely calls
add a comment |
This is a specification difference between the spread syntax for object literals and for array literals.
MDN briefly mentions it here -- I highlight:
Spread syntax (other than in the case of spread properties) can be applied only to iterable objects
The difference comes from the EcmaScript 2018 specification:
Concerning object spread syntax, see 12.2.6.8 Runtime Semantics: PropertyDefinitionEvaluation:
- It calls
CopyDataProperties(object, fromValue, excludedNames)
where the fromValue is wrapped to an object withToObject
, and therefore becomes iterable, even if fromValue is a primitive value likefalse
. Therefore...false
is valid EcmaScript.
- It calls
Concerning array spread syntax, see 12.2.5.2 Runtime Semantics: ArrayAccumulation:
- It merely calls
GetValue(spreadRef)
which does not do the above mentioned wrapping. And so the subsequent call toGetIterator
will trigger an error on a primitive value, as it is not iterable. Therefore[...false]
is invalid EcmaScript.
- It merely calls
This is a specification difference between the spread syntax for object literals and for array literals.
MDN briefly mentions it here -- I highlight:
Spread syntax (other than in the case of spread properties) can be applied only to iterable objects
The difference comes from the EcmaScript 2018 specification:
Concerning object spread syntax, see 12.2.6.8 Runtime Semantics: PropertyDefinitionEvaluation:
- It calls
CopyDataProperties(object, fromValue, excludedNames)
where the fromValue is wrapped to an object withToObject
, and therefore becomes iterable, even if fromValue is a primitive value likefalse
. Therefore...false
is valid EcmaScript.
- It calls
Concerning array spread syntax, see 12.2.5.2 Runtime Semantics: ArrayAccumulation:
- It merely calls
GetValue(spreadRef)
which does not do the above mentioned wrapping. And so the subsequent call toGetIterator
will trigger an error on a primitive value, as it is not iterable. Therefore[...false]
is invalid EcmaScript.
- It merely calls
edited May 7 at 11:55
answered May 7 at 11:44
trincottrincot
134k1697133
134k1697133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56021631%2fadd-elements-inside-array-conditionally-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not cleared. What do you want to achieve here? Can you please paste your
(condition && arr)
as well?– narayansharma91
May 7 at 11:34
FYI,
...null
and...undefined
won't throw error either. So, we can forgo that check as well if we are unsure if an object has value or not– adiga
May 7 at 12:13
1
FWIW if you wanted to spread booleans, you could do something like
Boolean.prototype[Symbol.iterator] = function* ()
(obviously don't do this in production code, it's a mere curio)– Conor O'Brien
May 7 at 14:00
Is what you really want to achieve
condition ? [...arr1, ...arr2] : arr1
or is this just a question on how the language works?– JollyJoker
May 7 at 14:20
2
@JollyJoker it's just a question on how the language works. Thanks
– João Rodrigues
May 7 at 14:34