JSON.parse fallback for undefined array to avoid exceptionDetecting an undefined object propertyCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?event.preventDefault() vs. return falseHow to determine if variable is 'undefined' or 'null'?Loop through an array in JavaScriptHow to check for “undefined” in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
What game is this character in the Pixels movie from?
Why do changes to /etc/hosts take effect immediately?
Boolean Difference with Offset?
Why do we use a cylinder as a Gaussian surface for infinitely long charged wire?
Most important new papers in computational complexity
If two black hole event horizons overlap (touch) can they ever separate again?
Balanced parentheses using STL C++
Can I travel from Germany to England alone as an unaccompanied minor?
Could a Weapon of Mass Destruction, targeting only humans, be developed?
Can two or more lightbeams (from a laser for example) have visible interference when they cross in mid-air*?
Buliding a larger matrix from a smaller one
Is this homebrew Half-Phoenix race balanced?
Find first and last non-zero column in each row of a pandas dataframe
Are all commands with an optional argument fragile?
How to test CRUD and FLS in unit tests?
How can I write a panicked scene without it feeling like it was written in haste?
Is it bad to describe a character long after their introduction?
How to securely dispose of a smartphone?
Is it okay to fade a human face just to create some space to place important content over it?
Meaning of じゃないんじゃない?
How do I tell the reader that my character is autistic in Fantasy?
Matrix decomposition
How did installing this RPM create a file?
Different budgets within roommate group
JSON.parse fallback for undefined array to avoid exception
Detecting an undefined object propertyCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?event.preventDefault() vs. return falseHow to determine if variable is 'undefined' or 'null'?Loop through an array in JavaScriptHow to check for “undefined” in JavaScript?How 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 margin-bottom:0;
I'm parsing my array and everything is OK if it is defined:
JSON.parse(myArray);
However I get an exception in case if myArray is undefined.
What is the best fallback for it, is there anything better than this:
JSON.parse(myArray || '[]');
similar like we first verify the object to avoid an exception if undefined
if (obj)
//do something with obj.something
So, is there anything shorter than
JSON.parse(myArray || '[]');
Thank you.
javascript arrays undefined fallback
add a comment |
I'm parsing my array and everything is OK if it is defined:
JSON.parse(myArray);
However I get an exception in case if myArray is undefined.
What is the best fallback for it, is there anything better than this:
JSON.parse(myArray || '[]');
similar like we first verify the object to avoid an exception if undefined
if (obj)
//do something with obj.something
So, is there anything shorter than
JSON.parse(myArray || '[]');
Thank you.
javascript arrays undefined fallback
2
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18
add a comment |
I'm parsing my array and everything is OK if it is defined:
JSON.parse(myArray);
However I get an exception in case if myArray is undefined.
What is the best fallback for it, is there anything better than this:
JSON.parse(myArray || '[]');
similar like we first verify the object to avoid an exception if undefined
if (obj)
//do something with obj.something
So, is there anything shorter than
JSON.parse(myArray || '[]');
Thank you.
javascript arrays undefined fallback
I'm parsing my array and everything is OK if it is defined:
JSON.parse(myArray);
However I get an exception in case if myArray is undefined.
What is the best fallback for it, is there anything better than this:
JSON.parse(myArray || '[]');
similar like we first verify the object to avoid an exception if undefined
if (obj)
//do something with obj.something
So, is there anything shorter than
JSON.parse(myArray || '[]');
Thank you.
javascript arrays undefined fallback
javascript arrays undefined fallback
asked Mar 25 at 13:15
HaradzieniecHaradzieniec
4,06620 gold badges86 silver badges174 bronze badges
4,06620 gold badges86 silver badges174 bronze badges
2
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18
add a comment |
2
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18
2
2
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18
add a comment |
1 Answer
1
active
oldest
votes
Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:
First, you could initialize myArray
with it defaulted to an empty array before it gets its values assigned.
var myArray = '[]';
Otherwise if myArray
is a parameter passed to a method you are parsing it from, you can default it in the arguments section.
function dosomething(myArray = '[]')
JSON.parse(myArray);
1
right, but it should bemyArray = "[]"
, not just[]
– georg
Mar 25 at 14:25
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%2f55338665%2fjson-parse-fallback-for-undefined-array-to-avoid-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:
First, you could initialize myArray
with it defaulted to an empty array before it gets its values assigned.
var myArray = '[]';
Otherwise if myArray
is a parameter passed to a method you are parsing it from, you can default it in the arguments section.
function dosomething(myArray = '[]')
JSON.parse(myArray);
1
right, but it should bemyArray = "[]"
, not just[]
– georg
Mar 25 at 14:25
add a comment |
Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:
First, you could initialize myArray
with it defaulted to an empty array before it gets its values assigned.
var myArray = '[]';
Otherwise if myArray
is a parameter passed to a method you are parsing it from, you can default it in the arguments section.
function dosomething(myArray = '[]')
JSON.parse(myArray);
1
right, but it should bemyArray = "[]"
, not just[]
– georg
Mar 25 at 14:25
add a comment |
Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:
First, you could initialize myArray
with it defaulted to an empty array before it gets its values assigned.
var myArray = '[]';
Otherwise if myArray
is a parameter passed to a method you are parsing it from, you can default it in the arguments section.
function dosomething(myArray = '[]')
JSON.parse(myArray);
Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:
First, you could initialize myArray
with it defaulted to an empty array before it gets its values assigned.
var myArray = '[]';
Otherwise if myArray
is a parameter passed to a method you are parsing it from, you can default it in the arguments section.
function dosomething(myArray = '[]')
JSON.parse(myArray);
edited Mar 28 at 12:18
answered Mar 25 at 13:21
Daniel DeesDaniel Dees
1372 silver badges11 bronze badges
1372 silver badges11 bronze badges
1
right, but it should bemyArray = "[]"
, not just[]
– georg
Mar 25 at 14:25
add a comment |
1
right, but it should bemyArray = "[]"
, not just[]
– georg
Mar 25 at 14:25
1
1
right, but it should be
myArray = "[]"
, not just []
– georg
Mar 25 at 14:25
right, but it should be
myArray = "[]"
, not just []
– georg
Mar 25 at 14:25
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55338665%2fjson-parse-fallback-for-undefined-array-to-avoid-exception%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
2
That seems pretty short as it is.
– Pointy
Mar 25 at 13:17
That's pretty much as good as you'll get short of modifying the lib function to check undefined
– SoZettaSho
Mar 25 at 13:18