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

attributes that contain arrays

1608411518
Joshua S.
Pro
Sheet Author
API Scripter
How do I create an attribute that contains something that will be read as an array by the sheetworker? I'm looking for something like: <input type="hidden" name="attr_array" value="[one, two, three]" /> ... ... ... getAttrs(["array"], v => {_.each(v.array, function(){...})} Basically, I just want to store an array in a hidden attribute and then later get it and do array things to it (like pop and push) and then re-store it. I'm sure this must be like 101 level stuff, but I'm barely a 100 level coder
1608412294
Kavini
Pro
Marketplace Creator
Sheet Author
Compendium Curator
You should be able to use JSON.parse to convert a string into an array (or an object).
1608421305
Oosh
Sheet Author
API Scripter
A comma is a common way to separate array items in a string. You can use the split() and join() methods to turn strings into arrays & back again. JSON.parse is definitely an option, but it does have strict some syntax required or it will throw an error. Split works like this: const hidden_attr = 'one,two,three'; let array1 = hidden_attr.split(','); // expected value of array1: ['one', 'two', 'three'] You don't have to use a comma to separate your values, it can be whatever you want (obviously a comma is inappropriate if you have actual sentences with punctuation stored in there, for example). To turn it back into a string so you can store it in a  character sheet again: const array2 = ['one', 'three']; let output = array2.join(','); // expected value of output: 'one,three' If you've got a more complex structure, say an array full of objects, then you'd want to go with JSON.parse and JSON.stringify as Nic suggested. In this case, you'd probably want to start with a working array/object structure and stringify it first, so you know you have a parseable layout to manipulate. Working the other way (trying to get a linear, working JSON structure typed out in an Attribute) could be difficult.