Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Any insight on my simple but utterly baffling error?

1542999415

Edited 1542999427
I have a fairly simple function that takes in a string of position parameters (e.g. "height: 200, width: 100, x: 10, y: 20"), and converts that into an object with the respective key/value pairs. Here is the code:      const options = {}     _ . each (params . split ( /,\s *? / gu ) , v => {                 options[v . split ( /\s *? :\s *? / gu )[ 0 ]] = parseInt (v . split ( /\s *? :\s *? / gu )[ 1 ] )      } )      D . Log ( ` Parsed Options: ${ JSON . stringify ( options ) }` )      D . Log ( ` Height: ${ JSON . stringify ( options . height) } ,               Width: ${ JSON . stringify ( options . width) } ,                   X: ${ JSON . stringify ( options . x) } ,                   Y: ${ JSON . stringify ( options . y) }` ) ("D.Log" is just a wrapper around log so that I can turn off debug reports with a global.) The weirdness:  When I pass in a string, say, "height: -33, width: -35, x: 0, y: 0", my log output reads thusly: "DERIVED OPTIONS: {height: -33, width: -35, x: 0, y: 0}" "Height: -33, Width: <UNDEFINED>, X: <UNDEFINED>, Y: <UNDEFINED>" The only difference between the two log entries is that I'm calling the properties individually in the second one, rather than outputting the whole object.  How could my options object have four valid properties when I stringify it the first time, but then three of the four properties are undefined on the very next line? I'm utterly baffled.
1543076993

Edited 1543087036
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
It's because you are creating the key as " width" instead of "width". This is because of your regex pattern where you are using "*?". This means it will match as few spaces as possible after the comma, minimum 0 and since you don't have anything required after the space it always matches 0 spaces making your split the same as if you just did /'/. Change your regex to just be /,\s*/ and you should be good. Also, the u tag is not supported in js as far as I know and when doing split, you don't need to use the global tag. Edit: you'll have a similar issue crop up in your split around the colon as well.