0
Array.from(document.querySelectorAll('.thread')).map((item) => {
    
    if(item.childNodes[1].childNodes[1] === undefined){
        console.log('hey')
    }
})

enter image description here

My point is, if there is no item.childNodes[1].childNodes[1] then do something. That’s it.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
questiontoansw
  • 307
  • 5
  • 17
  • [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) accepts a second argument that is the same as calling `.map`, but doesn’t create an intermediate array. – Sebastian Simon Feb 15 '21 at 20:59
  • Sorry i couldnt understand what you tried to say @SebastianSimon – questiontoansw Feb 15 '21 at 21:00
  • My English not so well to understand what you say can you edit my code instead? – questiontoansw Feb 15 '21 at 21:03
  • Are you aware that `childNodes` includes non-elements, e.g. Text nodes? `.children` contains only elements. If you’re sure about `.childNodes`, use [the optional chaining operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining). – Sebastian Simon Feb 15 '21 at 21:04
  • If you're targeting a modern browser, you can use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining): `if (item?.childNodes?.[1]?.childNodes?.[1] === undefined) {` – Heretic Monkey Feb 15 '21 at 21:05
  • [Check Here](https://stackoverflow.com/a/416327/10764341) to see if any of the methods work for you ... – Ashish Bhattarai Feb 15 '21 at 21:07

1 Answers1

0

You can make use of optional chaining :-

if(item?.childNodes?.[1]?.childNodes?.[1] === undefined){
        console.log('hey')
    }
Lakshya Thakur
  • 8,030
  • 1
  • 12
  • 39