This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 2 years ago.
what is the best aproach to check if a "deep" structure is undefined?
For example I have some check like this:
if (json.error.message != undefined) {
console.log(json.error.message);
}
The problem here is, that if json.error is already undefined this will crash. So I would need to write something like:
if (json.error != undefined && json.error.message != undefined) {
console.log(json.error.message);
}
But this could not be the best aproach or? When I would have even "deeper" structures.
if (json?.error?.message != undefined) {
console.log(json.error.message);
}
Related
This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.
This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 months ago.
I would like to simplify this expression but don't know how. There has to be a way to have the same expression without the three conditions.
if ( request.Document != null &&
request.Document != undefined &&
request.Document != "" )
Just for context I am talking something similar to c#'s !string.isNullOrEmpty()
if (request.Document) {
...
}
as null, undefined, '' will all return false.
(but [], {} or ' ' will return true)
Check the equality table for more info about other values.
This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 3 years ago.
Is there a concise or better way to write this condition without having to use so many ||.
if (myVar != 'A' && myVar != 'B' && myVar != 'C'){
...
}
You can also express it like an array,
if (!['A', 'B', 'C'].includes(myVar)) {
// if myVar is none of the values in the array
}
edit: added negation to grab the opposite case as your example
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
JavaScript, elegant way to check nested object properties for null/undefined [duplicate]
(4 answers)
Closed 5 years ago.
In JavaScript (or TypeScript), if I'm going to reference something like:
return myObj1.myObj2.myObj3.myProperty;
to be safe, I guard it with something like:
if (myObj1 && myObj2 && myObj3)
return myObj1.myObj2.myObj3.myProperty;
else
return null;
Is there a more concise way of just getting null from the above reference without surrounding it by an if?
Please note, since I'll be using this in a TypeScript app, some solutions may not work with dynamic object definition.
Thanks.
I'd use a library like lodash for this:
//gets undefined if any part of the path doesn't resolve.
const answer = _.get(myObj1, "myObj2.myObj3.myProperty");
//gets null if any part of the path doesn't resolve.
const answer2 = _.get(myObj1, "myObj2.myObj3.myProperty", null);
See https://lodash.com/docs/4.17.4#get for details.
You can define some temporary objects to substitute when a lookup fails.
return (((myObj1||{}).myObj2||{}).myObj3||{}).myProperty || null;
You can make a reusable object if desired.
const o = Object.create(null);
Then use it as needed.
return (((myObj1||o).myObj2||o).myObj3||o).myProperty || null;
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
Is there a short way for accessing a nested object without checking each parent object?
this will throw an exception if foo or bar is undefined:
var exists = (foo.bar.value !== undefined);
I would expect a check function like:
var exists = Object.exists(foo.bar.value);
Is there already something build-in javascript?
Use typeOf
if (typeof myObject!= "undefined") {
console.log('It exists')
}