This question already has answers here:
What's the simplest approach to check existence of deeply-nested object property in JavaScript? [duplicate]
(7 answers)
Closed 4 years ago.
This is my code snippet
pastActions = res['data']['list']['names']['blk'].cnt
But I get to see below error
its throws error as Cannot read property 'cnt' of undefined
This is because the parent of cnt is undefined, it might even fail when any of these properties data or list or names or blk is undefined.
I have replaced it as below
res['data'] && res['data']['list] && res['data']['list']['names'] && res['data']['list']['names']['blk'] && res['data']['list']['names']['blk'].cnt ? res['data']['list']['names']['blk'].cnt : '';
and it works, but is there any optimized way to check the same?
you can use lodash _.get function. You have an option to return a default value when you have any of the property in the object as undefined.
check here: lodash.com/docs/4.17.10#get
Related
This question already has answers here:
Access Javascript nested objects safely
(14 answers)
Closed 10 months ago.
I saw a function that receives a string parameter then performs some operations with it; like this:
const val = this.searchParam && this.searchParam.trim().toLowerCase();
My question is, why don't assign directly the processed string? Like this:
const val = this.searchParam.trim().toLowerCase();
I tried this in JS Bin to see if there's a difference, and the result is the same.
What do they exactly use the && operator?
In code snippet below, the first log writes undefined, the second throws an error:
searchParam = undefined
console.log(searchParam && searchParam.trim().toLowerCase());
console.log(searchParam.trim().toLowerCase());
Therefore, the result is not the same
This question already has answers here:
Safe navigation operator (?.) or (!.) and null property paths
(7 answers)
Closed 2 years ago.
I have a TypeScript code snippet that contains the following statement row?.delete();.
I was wondering what does the question mark symbolize?
What would happen in case row was null?
Thanks!
This operator is called optional chaining. What it does is first checks if the row is defined and then tries to access the delete.
In case row is null this row?.delete() will just return undefined.If you used it without this operator like row.delete() and row was null you would get Uncaught Type Error: Cannot read property 'row' of undefined.
For more details and examples you can check here https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
This question already has answers here:
Null-safe property access (and conditional assignment) in ES6/2015
(11 answers)
JS checking deep object property existence [duplicate]
(2 answers)
Closed 6 years ago.
I dont know if a.b is set. I want to do something only if a.b[0].c is true. I can do something like this:
if (a.b && a.b[0] && a.b[0].c) {
// ...
}
Is there a shortcut to check nested existence? Can I simplify this kind of condition?
I used to code golf, and one of the tricks we used is the following.
(myArray || [])[0] || 0
First, myArray || [] is evaluated, if myArray exists, this returns it, if it doesn't, then it returns an empty array. Let's say myArray isn't defined. Next, the [][0] || 0 expression gets evaluated ans because [][0] is undefined, this returns 0.
In your case it could be used the following way:
((a.b || [])[0] || {}).c || {}
This returns an empty object if something's undefined.
I'm not saying you should use this (in fact, you shouldn't), I just want to show you, that there is a smaller solution.
Update:
If tc39 gets through, then you'll have a much better solution using optional chaining:
a.b?[0]?.c
This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Next parameter is 'undefined' in jQuery, why? [duplicate]
(3 answers)
Closed 9 years ago.
In John Resig's slideshow on how he was building jQuery 1.4, he mentioned a point where he added an undefined variable to the jQuery closure because "we can re-use (the variable)".
undefined is not an ordinary variable:
> var undefined = 4
undefined
> undefined
undefined
Therefore, we know that undefined is not a variable. So why would an undefined be re-undefined in the jQuery source?
Because in some JavaScript engines it's possible to set undefined to a value. This is to make sure undefined is really undefined.
Additionally to +Rocket Hazmat's answer, you can reduce the file size after compression a bit, when your code uses undefined frequently. That's because a local variable undefined may have its name mangled by the compressor, while the global undefined may not:
foo === undefined;
// ^----- don't touch this, put "undefined" in the compressed result
(function (undefined) {
foo === undefined;
})();
// may however be mangled to
(function(u){foo===u})();
This question already has answers here:
Testing for an empty array object in JSON with jQuery
(5 answers)
Closed 8 years ago.
I have the following JSON object that is passed to one of my functions …
{"action":"setProjectAll", "application":{"proId":"new","zoomPosition":35,"scrollerPosition":0,"language":"en","timelinePosition":0}, "clips":[]}
How can I test this object for the propertie "clip" to be "[]" (empty)?
Right now in the example above it is empty, however since the object is dynamic I need to test for that property if it contains values or not.
How can that be done?
thank you
How about
if (x.clips && x.clips.length === 0)