Need an if operation, don't know how to do

Hi there,

I'm doing a geolocation lookup via Google API.

I get the MAC Addresses proper, but i need a function wich will lookup if the address has 12 characters, if not, i need a "0" in front of my string.

Sorry for the noob question :slight_smile:

What do you have so far to do the processing? Not sure if I know what you're asking. Adding 0 to a MAC address or what?

You do not need if, you can use padStart() or padEnd()

//str.padStart(targetLength, padString)
msg.payload = "12345678".padStart(9, "0");

If 9 characters in length no padding is added.

Hi there,

so my case would be:

msg.payload = "123456789as".padStart(11, "0");

?

Thanks!

You could experiment to find out, but if you want a string 12 chars long then set "12345678910".padStart(12,"0")This should output "012345678910".
Play around and try it.

i did, it works already, thanks everyone!

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