Skip to main content

Hi,

 

I’m hoping someone can help me. I’ve been playing around to try and do this, to mixed results.

The input values are coming in via webhook, as a list comma separated list


Example 1: Adding 2 to 1

Input 1: tAlice, Bob]

Input 2: tCharlie]

Desired Output: pAlice, Bob, Charlie]

 

Example 2: Subtract 2 from 1

Input 1: uAlice, Bob, Charlie]

Input 2: uBob]

Desired Output: tAlice, Charlie]

 

I’ve been playing around with code in both Javascript & Python to try and achieve these results.

I thought I had it, but instead of adding / subtract the items in the list, it seemed to add / delete the individual characters instead. But maybe I was outputting it wrong?

 

Thank you for any help you can offer.

 

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}];