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

sheetworker populate repeating element from array of objects

I have a fieldset named repeating_weap which I am trying to populate from a dropdown selection. This is a simplified version of the sheet worker, but I am trying to populate values in the repeating section from an array of objects. The code runs, but the values in the repeating section all end up as "undefined". I think I'm close to implementing this correctly, but I'm not sure what I'm missing.  on("change:thetype sheet:opened", function() {     getAttrs(["thetype"], function(values) {   let dsply = values.thetype; let A =  {name:"1", type:"melee", weapons:[ {name:"Dagger", damage:"1-4"}, {name:"Sword", damage:"1-8"}] }; let B =  {name:"2", type:"archer", weapons:[ {name:"Short Bow", damage:"1-6"}, {name:"Long Bow", damage:"1-8"}, {name:"Crossbow", damage:"1-8"}]  }; let charinfo = [A,B];         for (i = 0 ; i < charinfo.length ; i++) {             if (charinfo[i].name == dsply) {                 setAttrs({['type']: String(charinfo[i].type)}); const settings = {};  //create an object to store the new rows for (a in charinfo[i].weapons) { const newrowid = generateRowID();     settings["repeating_weap_" + newrowid + "_weaponname"] = String(a.name);     settings["repeating_weap_" + newrowid + "_weapondamage"] = String(a.damage); } setAttrs(settings);             }         }     }); });
1607917356

Edited 1607917730
Oosh
Sheet Author
API Scripter
You've actually got the weapons stored as an Array, so you're better off using a forEach loop there (the for...in loops is intended for key:value pairs in an object). You can also make it a little more efficient by storing everything in the 'settings' object you declared and just running setAttrs once - I don't write sheets, but my understanding it that minimising the use of getAttrs and setAttrs makes the sheet more responsive. I had to take some stuff out to run it in node, hopefully I put it back together properly: on("change:thetype sheet:opened", function () { getAttrs(["thetype"], function(values) { let dsply = values.thetype; let A = { name: "1", type: "melee", weapons: [{ name: "Dagger", damage: "1-4" }, { name: "Sword", damage: "1-8" }] }; let B = { name: "2", type: "archer", weapons: [{ name: "Short Bow", damage: "1-6" }, { name: "Long Bow", damage: "1-8" }, { name: "Crossbow", damage: "1-8" }] }; let charinfo = [A, B]; for (let i = 0; i < charinfo.length; i++) { if (charinfo[i].name == dsply) { let settings = {} //create an object to store the new rows settings['type'] = charinfo[i].type charinfo[i].weapons.forEach(weap => { let newrowid = generateRowID(); settings["repeating_weap_" + newrowid + "_weaponname"] = weap.name; settings["repeating_weap_" + newrowid + "_weapondamage"] = weap.damage; }) setAttrs(settings) } } }); }); The setAttrs should probably go outside the for() loop to ensure it only runs once, with a full object.... but I'm assuming it's impossible to have more than 1 match for charinfo[i].name == dsply, so it isn't a big deal here.
Thanks, that did it. I had tried doing something with a for loop like this:  for (let a = 0; a < charinfo[i].weapons.length; a++) but was still having trouble with the values being undefined. You're method of looping worked perfectly!