[英]Loop through array to find all objects with matching ids, and merge those objects into one element in the array.
[英]Code by Zapier to loop through an array of objects, pulling out one value per object and then average those values
將 JavaScript 用於 Zapier,我試圖從對象數組計算屬性的平均值。
這是其中一個對象...
{
"code": 0,
"data": [
{
"id": "28737",
"owner": "1",
"date": "1581945706",
"dla": "0",
"dlm": "1582551517",
"system_source": "3",
"source_location": null,
"ip_addy": null,
"ip_addy_display": null,
"import_id": "0",
"contact_cat": "*/*",
"bulk_mail": "1",
"bulk_sms": "0",
"bindex": "76",
"f1849": "9898983",
"f1850": "Foundation Course 2: Lecture 1 QUIZ",
"f1851": "0",
"f1853": "John Doe",
"f1854": "TRUE",
"f1855": "93", // <= calculate average for this property
"f1859": "292",
"f1862": "0",
"f1867": "Kajabi",
"f1868": "0",
"unique_id": "7WB77PT"
},
...
]
}
我需要為每個對象f1855
名為f1855
的屬性的值,然后計算平均值並通過 POST 返回該值。
我不認為用 JavaScript 很難做到這一點,但我不習慣 Zapier 的規則和限制。
任何幫助表示贊賞!
**看到您帖子的編輯版本,我現在不確定您的輸入數據是示例中的data
數組還是這些對象的數組。 如果是第二個,那么我不知道您是否想要每個對象的data
道具的平均值。 或者是其他東西。 但在任何一種情況下,下面的代碼都可以成為解決方案的一部分。
我對 Zapier 一無所知,但 JavaScript 部分可能如下所示:
const inputData = //your array of objects
const reducer = (sum, theObject) => sum + parseFloat(theObject.f1855)
const sumF1855 = inputData.reduce(reducer, 0)
const avgF1855 = sumF1855 / inputData.length
此代碼不處理錯誤條件(例如,如果某個對象缺少 f1855 屬性,或者如果 inputData 為空,則除以零)。 希望它能給你一個開始的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.