Help

Multiply an Array

3174 12
cancel
Showing results for 
Search instead for 
Did you mean: 
Carl_Duffaut
4 - Data Explorer
4 - Data Explorer

Hey there,

Is there a way to multiplyer an array of number to a constant or variable.

I have all my item with a default quantity on a table like so :
piecegroup.png

I’ve created group of item so I only have to add one group to list of the items with their default quantity (to prevent errors)

group.png

My problem is here on the packing List, when I want to be able to multiply the default quantity per Tower I need but I can’t figure out how to do so. How to multiply an array with “Tower needed” ?

packing list.png

12 Replies 12

It sounds like you hit one of Airtable’s idiosyncrasies when it comes to data being fed into an automation script. Long story short, even though there’s only a single record triggering the automation, the other data values—aside from the ID of the triggering record—often get passed as arrays, not single values. Because of this, you’ll need to convert the array to a string first before the split() method will work. One way is to use the same array-to-string conversion method that Airtable functions use: add the array to an empty string, though you’ll need to wrap that in parentheses before you can then use the split() method on it:

(singleAmount + "").split()

Two other options are to use either the join() or toString() array methods, both of which output a string:

singleAmount.join().split()
// ...or...
singleAmount.toString().split()
Julian_E_Post
8 - Airtable Astronomer
8 - Airtable Astronomer

Thanks very much! That was the info I needed. Here’s the script I ended up with:

// Setup
let table = base.getTable("Production Tracker")
let recordInput = input.config()
let singleAmount = recordInput.singleAmount
// Collect the list
let items =  (recordInput.singleAmount + "").split("\n")
let qty = recordInput.quantity
let newItems = []

// Calculate new totals
for (let item of items) {
    let parts = item.split(" ")
    let newQty = Number(parts[0]) * qty
    newItems.push(`${newQty} ${parts.slice(1).join(" ")}`)
}
output.set('Full Amount', newItems.join("\n"));

And the input variables:

Screen Shot 2022-01-07 at 3.55.13 PM