You are better off doing all the checking before you get to the setAttrs function, and using just one setAttrs. You will need to add all your header_image-pf_spell, et al to the getAttrs line. You could manually add them all, but this is a great opportunity for setting a variable to hold your values, and then constructing the function from them. You could convert your default values into an object like this const header_images = { 'header_image-pf_spell': "[default](<a href="http://imgur.com/9yjOsAD.png" rel="nofollow">http://imgur.com/9yjOsAD.png</a>)", 'header_image-pf_attack-melee': "[default](<a href="http://i.imgur.com/AGq5VBG.png" rel="nofollow">http://i.imgur.com/AGq5VBG.png</a>)", 'header_image-pf_attack-dual': "[default](<a href="http://i.imgur.com/Eh243RO.png" rel="nofollow">http://i.imgur.com/Eh243RO.png</a>)", 'header_image-pf_attack-ranged': "[default](<a href="http://imgur.com/58j2e8P.png" rel="nofollow">http://imgur.com/58j2e8P.png</a>)", 'header_image-pf_attack-cmb': "[default](<a href="http://i.imgur.com/Si4vfts.png" rel="nofollow">http://i.imgur.com/Si4vfts.png</a>)", 'header_image-pf_defense': "[default](<a href="http://imgur.com/02fV6wh.png" rel="nofollow">http://imgur.com/02fV6wh.png</a>)", 'header_image-pf_generic': "[default](<a href="http://imgur.com/phw1eFB.png" rel="nofollow">http://imgur.com/phw1eFB.png</a>)", 'header_image-pf_ability': "[default](<a href="http://i.imgur.com/UxYSva8.png" rel="nofollow">http://i.imgur.com/UxYSva8.png</a>)", 'header_image-pf_block': "[default](<a href="http://imgur.com/nBnv4DL.png" rel="nofollow">http://imgur.com/nBnv4DL.png</a>)", 'header_image-pf_generic-skill': "[default](<a href="http://imgur.com/8dCkRtG.png" rel="nofollow">http://imgur.com/8dCkRtG.png</a>)", 'header_image-pf_generic-init': "[default](<a href="http://i.imgur.com/pjS6HVJ.png" rel="nofollow">http://i.imgur.com/pjS6HVJ.png</a>)", 'header_image-pf_block-item': "[default](<a href="http://i.imgur.com/4FgQuqS.png" rel="nofollow">http://i.imgur.com/4FgQuqS.png</a>)", 'header_image-pf_block-check': "[default](<a href="http://i.imgur.com/a6O3ZGB.png" rel="nofollow">http://i.imgur.com/a6O3ZGB.png</a>)" }; Then you need to build the getAttribute line. You can do what I'm about to do in much more compact code, but I'm doing it step by step for clarity. There's a function Object.keys(variable name) which will give you an array of the names on the left side of : in the above object. so we can create a variable to hold that. const header_image_names = Object.keys(header_images); Now there's a problem here, in that we'll also need the "header_images_toggle". We could add that into the array above using push, but that will cause us problems later. So we'll manually add it as we need below. We can use that array directly on the getAttr line, like so getAttrs(header_image_names, function(values) { // no need for [ ] since this is already an array. Since we also need header_images_toggle, we'll push it in here getAttrs(header_image_names.push('header_images_toggle'), function(values) { // no need for [ ] since this is already an array. We could also use concat (combine two arrays), if we convery header_images_toggle into an array, like so getAttrs(header_image_names.concat(['header_images_toggle']), function(values) { There's always multiple ways to do things. I'm assuming you only want this worker to run when you change the header_images_toggle, and not when any of the individual images are changed, so we don't need to change the on('change row. So we can start building the sheet worker. Our starting code looks like this: const header_images = { 'header_image-pf_spell': "[default](<a href="http://imgur.com/9yjOsAD.png" rel="nofollow">http://imgur.com/9yjOsAD.png</a>)", 'header_image-pf_attack-melee': "[default](<a href="http://i.imgur.com/AGq5VBG.png" rel="nofollow">http://i.imgur.com/AGq5VBG.png</a>)", 'header_image-pf_attack-dual': "[default](<a href="http://i.imgur.com/Eh243RO.png" rel="nofollow">http://i.imgur.com/Eh243RO.png</a>)", 'header_image-pf_attack-ranged': "[default](<a href="http://imgur.com/58j2e8P.png" rel="nofollow">http://imgur.com/58j2e8P.png</a>)", 'header_image-pf_attack-cmb': "[default](<a href="http://i.imgur.com/Si4vfts.png" rel="nofollow">http://i.imgur.com/Si4vfts.png</a>)", 'header_image-pf_defense': "[default](<a href="http://imgur.com/02fV6wh.png" rel="nofollow">http://imgur.com/02fV6wh.png</a>)", 'header_image-pf_generic': "[default](<a href="http://imgur.com/phw1eFB.png" rel="nofollow">http://imgur.com/phw1eFB.png</a>)", 'header_image-pf_ability': "[default](<a href="http://i.imgur.com/UxYSva8.png" rel="nofollow">http://i.imgur.com/UxYSva8.png</a>)", 'header_image-pf_block': "[default](<a href="http://imgur.com/nBnv4DL.png" rel="nofollow">http://imgur.com/nBnv4DL.png</a>)", 'header_image-pf_generic-skill': "[default](<a href="http://imgur.com/8dCkRtG.png" rel="nofollow">http://imgur.com/8dCkRtG.png</a>)", 'header_image-pf_generic-init': "[default](<a href="http://i.imgur.com/pjS6HVJ.png" rel="nofollow">http://i.imgur.com/pjS6HVJ.png</a>)", 'header_image-pf_block-item': "[default](<a href="http://i.imgur.com/4FgQuqS.png" rel="nofollow">http://i.imgur.com/4FgQuqS.png</a>)", 'header_image-pf_block-check': "[default](<a href="http://i.imgur.com/a6O3ZGB.png" rel="nofollow">http://i.imgur.com/a6O3ZGB.png</a>)" }; const header_image_names = Object.keys(header_images); on(change:header_images_toggle , function() { getAttrs(header_image_names.push('header_images_toggle'), function(values) { Now we can look at building the actual worker itself. We need to get the values of all the header images, and we can create a new object for that purpose. const tempToggle = values.header_images_toggle; let tempImages = {}; header_image_names .forEach( image => { tempImages[image] = values[image]; } What we are doing here is looping through each attribute in the header_image_names (which, if you remember, is a list of all the header image attribute names). We get their values and add them to our tempImages object. It'll look a lot like the header_images object from the start of the code, except it will have the current value of each item. The tricky part is probably tempImages[image] = values[image]; On the first loop, image will be 'header_image-pf_spell'. So this is equivalent to: tempImages['header_image-pf_spell'] = values['header_image-pf_spell']; It's basically saying, get the value of header_image-pf_spell, and create a new item in the tenpImages object whose name is header_image-pf_spell, and whose value is that attribute's value. And at the end of the loop, you end up with an object which has all the current attribute values, stored with their name. If you're more comfortable with for loops, the above is equivalent to this, but forEach is easier to write once youre familiar with it. const tempToggle = values.header_images_toggle; let tempImages = {}; for(let i = 0; i < header_image_names.length; i++) { tempImages[header_image_names[i]] = values[header_image_names[i]]; } Now, something I want to draw your attention to, if you're not already aware of it. Look at the first setAttrs in your original code. Compare it to the header_images object i created at the start of this post. Notice how they are almost identical? This means if I did this setAttrs(header_images); it would be a valid instruction, and would set all those values, because header_images is in the format setAttrs needs. This lets us do something clever. We have already built an object in that format, using the loop above. What can do is build that object in a different way - to only include the attributes that need to change. it would look like this: const tempToggle = values.header_images_toggle; let tempImages = {}; header_image_names.forEach( image => { // we loop through all the attribute names const currentImage = values[image]; // in each loop, we grab the current attribute value, using its name
if(currentImage === header_images[image] || currentImage === '') { // compare the image, and if it is neither the default or '', skip it
if(tempToggle > 0) { // if tempToggle is 0, store the default value, otherwise store the null value.
tempImages[image] = values[image];
} else {
tempImages[image] = '';
}
}
}
setAttrs(tempImages); // we have a list of the objects that can change, and their values, and we assign them. There's a lot that happened in this above code. We loop through all the attribute names, and we grab each one in turn and look at its value. if(currentImage === header_images[image] || currentImage === '') { If it's value is the default or null, we proceed, otherwise we skip it. If we proceed, if(tempToggle > 0) { tempImages[image] = values[image]; } else { tempImages[image] = ''; } You can also write this code in more compact form, like this tempImages[image] = (tempToggle > 0 ? values[image] : ''); The conclusion is, when we run setAttrs, it only updates the attributes we want it to, and ignores the rest. Here is the resulting complete code: const header_images = { 'header_image-pf_spell': "[default](<a href="http://imgur.com/9yjOsAD.png" rel="nofollow">http://imgur.com/9yjOsAD.png</a>)", 'header_image-pf_attack-melee': "[default](<a href="http://i.imgur.com/AGq5VBG.png" rel="nofollow">http://i.imgur.com/AGq5VBG.png</a>)", 'header_image-pf_attack-dual': "[default](<a href="http://i.imgur.com/Eh243RO.png" rel="nofollow">http://i.imgur.com/Eh243RO.png</a>)", 'header_image-pf_attack-ranged': "[default](<a href="http://imgur.com/58j2e8P.png" rel="nofollow">http://imgur.com/58j2e8P.png</a>)", 'header_image-pf_attack-cmb': "[default](<a href="http://i.imgur.com/Si4vfts.png" rel="nofollow">http://i.imgur.com/Si4vfts.png</a>)", 'header_image-pf_defense': "[default](<a href="http://imgur.com/02fV6wh.png" rel="nofollow">http://imgur.com/02fV6wh.png</a>)", 'header_image-pf_generic': "[default](<a href="http://imgur.com/phw1eFB.png" rel="nofollow">http://imgur.com/phw1eFB.png</a>)", 'header_image-pf_ability': "[default](<a href="http://i.imgur.com/UxYSva8.png" rel="nofollow">http://i.imgur.com/UxYSva8.png</a>)", 'header_image-pf_block': "[default](<a href="http://imgur.com/nBnv4DL.png" rel="nofollow">http://imgur.com/nBnv4DL.png</a>)", 'header_image-pf_generic-skill': "[default](<a href="http://imgur.com/8dCkRtG.png" rel="nofollow">http://imgur.com/8dCkRtG.png</a>)", 'header_image-pf_generic-init': "[default](<a href="http://i.imgur.com/pjS6HVJ.png" rel="nofollow">http://i.imgur.com/pjS6HVJ.png</a>)", 'header_image-pf_block-item': "[default](<a href="http://i.imgur.com/4FgQuqS.png" rel="nofollow">http://i.imgur.com/4FgQuqS.png</a>)", 'header_image-pf_block-check': "[default](<a href="http://i.imgur.com/a6O3ZGB.png" rel="nofollow">http://i.imgur.com/a6O3ZGB.png</a>)" }; const header_image_names = Object.keys(header_images); on(change:header_images_toggle , function() { getAttrs(header_image_names.push('header_images_toggle'), function(values) { const tempToggle = values.header_images_toggle;
let tempImages = {};
header_image_names.forEach( image => { const currentImage = values[image];
if(currentImage === header_images[image] || currentImage === '') {
tempImages[image] = (tempToggle > 0 ? currentImage : '');
}
setAttrs(tempImages);
}); }); Now I hit send, and hope I havent made a typo somewhere... Edit: changed array name from header_worker_array to header_image_names for clarity. Edit 2: and fixed the Object.keys line and getAttrs line!