What does this code?

var ssids = msg.payload.split('\n').filter(s => !!s)

This is one line from the wifi setup flow provided here from flows.
I have no idea how to interpret this. Is it just for confusing the aliens?

It would help to provide a link.

It looks like it is taking multi-line text output, creating an array with an entry for each line and then only returning certain elements of the array. I'd need to know the input format to work out what the !! is doing.

! is a logical NOT in JavaScript so !! is doing a double not.

Logical NOT (!) - JavaScript | MDN (mozilla.org)

From what I can tell, !!s will always return true?

Google! Wifi Settings UI Dashboard (flow) - Node-RED (nodered.org)

And it looks like that code doesn't actually work anyway - not on my system anyway

Assuming msg.payload is a string, it is splitting that into an array of lines & filtering out empty strings.

  • split turns a string into an array of split parts (e.g splitting on \n means each line)
  • filter is an array function that filters out items based on criteria
  • !!s is the same as saying "not not s"
    So if s is empty, not s would be true and and not true will be false
    it is called double bang...
    ! true ; // Returns false .
    ! false ; // Returns true .
    !!false ; // Returns false .
    !!"" ; // Returns false .

demo - note how "line 3" is empty - it gets filtered out...


Run on my Pi, the command returns the list of SSID's.

In that case, the line in question removes blank entries from the ssids array. There are much better ways to do that assuming you think that better = comprehensible.

You might want to also filter out SSID's such as \x00\x00\x00\x00\x00\x00\x00\x00\x00 which appears twice in the SSID's my Pi3 can see.

Thanks for the answers. Code works for me on raspi, did not know that this double NOT is used for filtering blanc inputs.

Which is why I would advise against using it. Use some code that you will recognise when you come back to it in a year's time.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.