I'm using javascript's .find() method to search for values in my array. I am getting the following error: Uncaught TypeError: Cannot read property 'toUpperCase' of null which is occurring at this line: if (this.collection.find(x => x.details.numb.toUpperCase() === numb)) {
I believe this error is happening because there are null values coming in at x.details.numb. Is it possible to ignore null values from within this line of code? Or will I have to remake my array without null values?
You can check for null values before calling toUpperCase
if (this.collection.find(x => x.details && x.details.numb && x.details.numb.toUpperCase() === numb)) {
As #VLAZ pointed out in comments this will fail for numb === '' in that case you can use
(x.details && x.details.numb || '' ).toUpperCase() === numb
Sure !
x.details && x.details.numb && x.details.numb.toUpperCase() === numb
Just use a check for null, as well as toUpperCase:
this.colleciton.find(x => x.details != null && x.details.numb != null && x.details.numb.toUpperCase && x.details.numb.toUpperCase() === numb)
Since you're in a condition, you can use some instead of find, because some stops once the condition is met (resulting in better performance) :
this.collection
.some(item => item && item.details && items.details.numb.toUpperCase() === numb)
As brought up by a wild commenter, you can also prevent undefined values from happening as such :
this.collection
.some(item => item
&& item.details
&& items.details.numb
&& ![undefined, null].includes(items.details.numb)
items.details.numb.toUpperCase() === numb)
Related
I am having an issue to understand this:
!fullResponse?.response?.is_complete
I was thinking it is the same as
fullResponse &&
fullResponse.response &&
'is_complete' in fullResponse.response &&
!fullResponse.response.is_complete
but it is not and it breaks my code specially when is_complete does not present in fullResponse.response
Can anyone explain what this does : !fullResponse?.response?.is_complete and if there is a way to make it act as below?
fullResponse &&
fullResponse.response &&
'is_complete' in fullResponse.response &&
!fullResponse.response.is_complete
The part you've probably misunderstood is the precedence of these operators. Your code actually boils down to:
!(
// vvvvvvv--- `== null`: either null or undefined
(fullResponse == null) ? undefined
:(fullResponse.response == null) ? undefined
:fullResponse.response.is_complete
)
So, when either part of your lookup short-circuits with an undefined, it runs right into the negation and gets converted to true.
If you just want to return false in case of a short-circuit, then it's as easy as:
// vvvv--- this will turn into false
!(fullResponse?.response?.is_complete ?? true)
However, if you want undefined in this case, it's easier with a variable:
const isComplete = fullResponse?.response?.is_complete
isComplete === undefined ? undefined : !isComplete
If you can't do that, you'll have to repeat the lookup:
fullResponse?.response?.is_complete === undefined ? undefined : !fullResponse?.response?.is_complete
I had a bug that turned out to be because I forgot to check for null values. I added the check, but would like if it could be written more concise.
<ListItem onClick={!pattern.capped && !pattern.hasInlineRef ? search(pattern.dbValue, pattern.dbValue === "" || pattern.dbValue === null) : undefined}>
Can this check for empty or null be shortened?
search(pattern.dbValue, pattern.dbValue === "" || pattern.dbValue === null)
It's written in Typescript, but I guess the curly braces make it JavaScript.
First, you can create an util function to check null, empty or undefined:
export function isNullOrEmpty(value) {
return (value === null || value === undefined || value === "") ? true : false
}
Or, you can fast test with falsy and truthy:
search(pattern.dbValue, !pattern.dbValue)
List of falsy values in JavaScript: MDN Falsy
false
null
undefined
0
NaN
''
document.all
You can create an helper function in a separate folder and pull out this function into the files where you need to check for null, undefined, empty string or an empty object.You can write something like this:
export default value =>
value === undefined ||
value === null ||
(typeof value === 'object' && Object.keys(value).length === 0) ||
(typeof value === 'string' && value.trim().length === 0);
Hope this helps
I'm checking if the value of cookie is undefined or null but weirdly this does not really work
Here's what I tried
function check(val){
if($.cookie(val) === "undefined" || $.cookie(val) === "null"){
return "1";
}
return $.cookie(val);
}
So even if my cookie has value null it would print out null instead of 1
That is because you are using quotes. In other words, "undefined" === undefined is false.
if($.cookie(val) === undefined || $.cookie(val) === null){ //this would be more apropriate
Please check if jquery cookie library is included , this is the most general cause.
I have a string like this:
var TheDate = "6.14.2012";
and I have an object MyObject that has properties that may match that string. However, when I write this:
if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }
the conditions never seem to evaluate to true even though I know the property doesn't exist.
What am I missing??
Thanks.
Existence of Own Property
If you want to check for the existence of a property use .hasownProperty()...
if (MyObject.hasOwnProperty(TheDate)) {
Existence of Own Or Inherited Property
If the property may be inherited, to test for existence use the in operator...
if (TheDate in MyObject) {
null or undefined value test for Own Or Inherited Property
If it's not existence, but rather a value test, and you want to test for null or undefined, then use this...
if (MyObject[TheDate] == null) {
This will check for both null and undefined.
The problem is with the quotes around undefined ;)
Change
if (MyObject[TheDate] === null || MyObject[TheDate] === 'undefined') { .... }
to
if (MyObject[TheDate] === null || MyObject[TheDate] === undefined) { .... }
It should work :)
If you want to use the string "undefined" rather than the undefined object, the way to do this is to check the "typeof", which returns a string. In this case, if you're looking to test if it's set to "undefined":
if ( MyObject[TheDate] === null || typeof MyObject[TheDate] === 'undefined' ) { .... }
Try the following
if ( (MyObject[TheDate] === null) || (typeof MyObject[TheDate] === 'undefined') {
....
}
You were checking whether your property contains the String 'undefined' and not whether its value is currently undefined.
I have the following javascript code:
var currentIds = localStorage.getItem('currentPairsIds');
if ((typeof currentIds === "undefined") ||
(currentIds == null))
$.myNameSpace.currentIDs = new Array(3);
else
$.myNameSpace.currentIDs = currentIds.Split(',');
I'm debugging with Firebug and although currentIds hasn't got any value it always executes else statement.
UPDATE: I'm getting this value from HTML5 storage.
What am I doing wrong?
This is how I have solved my problem:
var currentIds = localStorage.getItem('currentPairsIds');
if ((currentIds === undefined) ||
(currentIds == null) || (currentIds == "undefined"))
$.myNameSpace.currentIDs = new Array(3);
else
$.myNameSpace.currentIDs = currentIds.split(',');
localStorage.getItem('currentPairsIds'); returns the string "undefined".
There is another error in Split() function. The right version is without any capital letter.
I would use a direct comparison instead of the notoriously odd "typeof" operator:
if ((currentIds === undefined) || (currentIds === null)) {
//...
It's not working because localStorage.getItem returns null if the item is not defined, it does not return undefined http://dev.w3.org/html5/webstorage/#dom-storage-getitem
Example: http://jsfiddle.net/mendesjuan/Rsu8N/1/
var notStored = localStorage.getItem('ffff');
alert(notStored); // null
alert(typeof notStored); // object, yes, null is an object.
Therefore you should just be testing
alert(notStored === null);
I think you have to make checking for undefined comparing with == instead of ===.
Example:
typeof currentIds == "undefined"
This will make sure, the variable is really undefined or not.
[Edit Edit Edit Edit :P]
currentIds = "undefined"
implies
typeof currentIds == "String"
Also see, Detecting Undefined, === isn't necessary for string comparison.
In my case LocalStorage.getItem() was converting it to "undefined" as string.
I also needed to check if it s "undefined" as a string.
var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
...
}
if( typeof(varName) != "undefined" && varName !== null )
be sure, you use ( " " ) quotes when checking with typeof()