How to specify the operator?

lalka function() {
// Get array of elements
var stickerArray = Array.from( document.querySelectorAll('.sticker') );

// If at least one element is in the array
if ( stickerArray.length ) {

// Go through cycles on all elements, do what we need
 stickerArray.forEach( function (sticker) {
 var stickerTitle = sticker.getAttribute('title');
 var arr = stickerTitle.split('\n').filter(str=>{return str.length > 0})
 var n = arr.find(bbb => -1 != bbb.indexOf('Katowice 2014'))
// I tried to specify different conditions for the if, the code is not working
if ( n.length > 0 ) {
 if (Notification.permission !== "granted")
Notification.requestPermission();
 else {
 var notification = new Notification('Notification title', {
 icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
 body: "EZ KATOWICA 2014",
});
 window.open("https://opskins.com/?loc=shop_browse&app=730_2"); 
}
}
});
 // Go through cycles on all elements, do what we need
 stickerArray.forEach( function (sticker) {
 var stickerTitle = sticker.getAttribute('title');
 var arr = stickerTitle.split('\n').filter(str=>{return str.length > 0})
 var nnn = arr.find(bbb => -1 != bbb.indexOf('Crown (Foil)'))
 if ( n.length ) {if (Notification.permission !== "granted")
Notification.requestPermission();
 else {
 var notification = new Notification('Notification title', {
 icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
 body: "EZ KATOWICA 2014",
});
 window.open("https://opskins.com/?loc=shop_browse&app=730_2"); 
 } 
});
}
}
setInterval(lalka, 5000);

If, instead if ( n.length > 0 ) { I output the variable n in the console, then everything works, I get a text.
The if condition is executed if you execute a separate script. That is, the problem is that I do not correctly indicate the condition of this operator
June 10th 19 at 16:35
1 answer
June 10th 19 at 16:37
Solution
The find() method returns the first value found in the array item which satisfies the condition passed in the callback function, or undefinedif no such element is found.
Then you try to get length from undefined, which causes the error.

Replace the check for n !== 'undefined' or use the findIndex(), which returns index of found element or -1 if none is found.

Find more questions by tags ParsingJavaScript