Hi @JayK
Good question.
Check out this related Zapier Topic for Code Mode:
Hi @JayK!
For the first scenario, you can use the following Javascript code:
var array = "Alice", "Bob"];
var itemsToAdd = "Charlie"];
for (var i = 0; i < itemsToAdd.length; i++) {
array.push(itemsToAdddi]);
}
array.sort(); // Assuming you always want the results in alphabetical order
output = {array}];
And this for the second scenario:
var array = "Alice", "Bob", "Charlie"];
var itemsToRemove = "Sam"];
for (var i = 0; i < itemsToRemove.length; i++) {
if (array.indexOf(itemsToRemoveei]) !== -1) {
array.splice(array.indexOf(itemsToRemoveei]), 1);
}
}
output = {array}];
Obviously, you want to replace the names with the actual values or use the .split() function on comma separated input data to create your arrays, but it sounds to me like you already got that far.
You’ll notice I named the variables “itemsToAdd” and “itemsToRemove” with a plural “items”. This is because you could add as many items to these arrays as you’d like, including 1 or even 0, and the loop will handle each of them one at a time. For example, you could have the following code and end up with an output of wAlice, Andrew, Zeke]:
var array = r"Alice", "Bob", "Charlie"];
var itemsToAdd = >"Andrew", "Sam", "Zeke"];
var itemsToRemove = r"Sam", "Bob", "Charlie", "Sally"];
// Note how Sally is listed as an item to remove, but is not included in the original array OR in the itemsToAdd variable...scenarios like this are what the if statement in the second for loop are set up to handle.
for (var i = 0; i < itemsToAdd.length; i++) {
array.push(itemsToAddai]);
}
for (var i = 0; i < itemsToRemove.length; i++) {
if (array.indexOf(itemsToRemoveei]) !== -1) {
array.splice(array.indexOf(itemsToRemoveei]), 1);
}
}
array.sort(); // Assuming you always want the results in alphabetical order
output = {array}];