![](/img/trans.png)
[英]Look up values in an array using looping forEach Google Apps Script Javascript
[英]Look up value from array in another array, and then match a second value Google Apps Script Javascript JSON API
我正在使用 Google Apps Script 將數據從我的 crm Pipedrive 導入 Google 表格。 這是更大過程的一部分,但我對腳本的這一部分陷入僵局。 我需要通過將一個數組的兩個部分與另一個數組進行匹配來返回一個值。
首先,我提取所有交易字段,它返回自定義字段鍵及其 ID/標簽對。 這是一個簡化的輸出示例:
{
"success": true,
"data": [
{
"id": 12500,
"key": "c4ecbe01c34994ede3a50c0f8",
"name": "Lead Type",
"options": [
{
"label": "Expired",
"id": 28
},
{
"label": "Sale",
"id": 29
},
{
"label": "Rent",
"id": 30
},
{
"label": "Other",
"id": 31
}
],
"mandatory_flag": false
}
]
}
然后我從包含 id 的特定交易中獲得單獨的信息。 我需要將下面的 id 28
與上面的數組匹配並返回標簽Expired
: var leadType = dealresponse.data["c4ecbe01c34994ede3a50c0f8"];
它返回28
我不知道 '28' 是什么意思,所以這就是為什么我需要將它與標簽 Expired 匹配。 dealFields 數組很長,可能是上述數組對象的 50 或 100 個。 大約有 10 個自定義交易字段鍵,我將不得不根據匹配鍵和 ID 返回標簽。 我想我必須循環每個鍵和 id 才能返回標簽。 但不確定執行此操作並節省處理能力的最佳方法。
我試過:
for (var i in dealFieldsresponse) {
if (dealFieldsresponse[i].data.key == "c4ecbe01c34994ede3a50c0f8") {
for (var j in dealFieldsresponse[j]) {
if (dealFieldsresponse[j].id == "28") {
Logger.log(dealFieldsresponse[j].label);
}
}
}
}
它不起作用。 我是 javascript 和編程的新手,所以這是我最好的猜測,我很感激任何見解。
編輯:這是我必須使用的更大的代碼塊:
// Get deal fields data
var dealFieldsurl = URL +'/v1/dealFields?api_token='+ API_TOKEN;
var options = {
"method": "get",
"contentType": "application/json",
};
var dealFieldsresponse = UrlFetchApp.fetch(dealFieldsurl, options);
dealFieldsresponse = JSON.parse(dealFieldsresponse.getContentText());
// Get deal data
var dealurl = URL +'/v1/deals/' + dealId + '?api_token='+ API_TOKEN;
var options = {
"method": "get",
"contentType": "application/json",
};
var dealresponse = UrlFetchApp.fetch(dealurl, options);
dealresponse = JSON.parse(dealresponse.getContentText());
var propertyAddress = dealresponse.data["9bd1d8c4f07f5795fd8bffb16f3b63c6547d7d3a"];
var leadType = dealresponse.data["c4ecbe01c3494d1be52432f4a3194ede3a50c0f8"];
var dealType = dealresponse.data["a4269fb4730cf7fd1787752be94eacbc4b0de24e"];
var dealSource = dealresponse.data["d76fa2d6f8454a51f7d64d981cd9320877bc2ea0"];
var marketingFor = dealresponse.data["58cb55090b55652b7f89a8b44074682d874c548a"];
var dateListedOnMarket = dealresponse.data["aa49c7b95a7d151bec4c2d936f6ab40d0caea43c"];
var dateTakenOffMarket = dealresponse.data["660c1250b0a641a10ff9121c2df124ff89c13052"];
var askingPrice = dealresponse.data["1de94dbf589fda7a3a3248662cd24f03d512a961"];
dealFieldsresponse
變量存儲一個數組,其中包含許多包含數組的對象。 這是兩個主要對象,您可以看到每個對象都有一個key
和options
。 我需要匹配密鑰,然后在每個key
選項中找到id
{
"id": 12500,
"key": "c4ecbe01c3494d1be52432f4a3194ede3a50c0f8",
"name": "Lead Type",
"order_nr": 64,
"field_type": "set",
"add_time": "2020-08-20 19:33:22",
"update_time": "2020-08-20 19:33:22",
"last_updated_by_user_id": 11678191,
"active_flag": true,
"edit_flag": true,
"index_visible_flag": true,
"details_visible_flag": true,
"add_visible_flag": true,
"important_flag": true,
"bulk_edit_allowed": true,
"searchable_flag": false,
"filtering_allowed": true,
"sortable_flag": true,
"options": [
{
"label": "Expired",
"id": 28
},
{
"label": "Sale",
"id": 29
},
{
"label": "Rent",
"id": 30
},
{
"label": "Other",
"id": 31
}
],
"mandatory_flag": false
},
{
"id": 12502,
"key": "a4269fb4730cf7fd1787752be94eacbc4b0de24e",
"name": "Deal Type",
"order_nr": 65,
"field_type": "set",
"add_time": "2020-08-20 19:57:12",
"update_time": "2020-08-20 19:57:12",
"last_updated_by_user_id": 11678191,
"active_flag": true,
"edit_flag": true,
"index_visible_flag": true,
"details_visible_flag": true,
"add_visible_flag": true,
"important_flag": true,
"bulk_edit_allowed": true,
"searchable_flag": false,
"filtering_allowed": true,
"sortable_flag": true,
"options": [
{
"label": "Lease",
"id": 37
},
{
"label": "Financing",
"id": 38
},
{
"label": "Assign",
"id": 39
},
{
"label": "ST",
"id": 40
},
{
"label": "Other (see notes)",
"id": 41
}
],
"mandatory_flag": false
},
編輯 2:如何返回多個 ID 的標簽?
const obj = {
"a4269fb4730cf7fd1787752be94eacbc4b0de24e": {id: 37,38}, "58cb55090b55652b7f89a8b44074682d874c548a": {id: 44,45},
"2ec54cce0d091b69b1fd1a245c7aad02b57cadb8": {id: 126},
"fab84c732295022ecd7bdf58892a62cb4d8ecf24": {id: 50,52,54},
};
例如,我希望第一個返回red, blue
作為字符串,第二個返回green, orange
作為字符串。 假設與id
匹配的標簽是顏色。 第三個只有一個id
,但第四個有三個。 我該如何解釋? 我希望我的輸出是某種數組,然后我可以說搜索key
a4269fb4730cf7fd1787752be94eacbc4b0de24e
並返回值red, blue
作為字符串
我相信你的目標如下。
key
和id
檢索label
的值。
"key": "c4ecbe01c34994ede3a50c0f8"
和"id": 28
檢索"label": "Expired"
的值。data
和options
數組。 兩個數組都有幾個元素。dealFieldsresponse
是您問題中的 JSON 對象,則dealFieldsresponse.data
和dealFieldsresponse.data[].options
是一維數組。 當您要檢索key
和id
的值時,需要循環這些數組。當以上幾點反映到你的腳本中時,它變成如下。
const searchKey = "c4ecbe01c34994ede3a50c0f8"; // Please set the value of key. const searchId = 28; // Please set the value of id. const dealFieldsresponse = { "success": true, "data": [ { "id": 12500, "key": "c4ecbe01c34994ede3a50c0f8", "name": "Lead Type", "options": [ { "label": "Expired", "id": 28 }, { "label": "FSBO", "id": 29 }, { "label": "FRBO", "id": 30 }, { "label": "Other", "id": 31 } ], "mandatory_flag": false } ] }; const data = dealFieldsresponse.data; for (let i = 0; i < data.length; i++) { if (data[i].key == searchKey) { const options = data[i].options; for (let j = 0; j < options.length; j++) { if (options[j].id.toString() == searchId.toString()) { // Logger.log(options[j].label); console.log(options[j].label); } } } }
作為其他示例腳本,下面的腳本怎么樣? 在此示例中,結果值放在一個數組中。
const searchKey = "c4ecbe01c34994ede3a50c0f8"; // Please set the value of key. const searchId = 28; // Please set the value of id. const dealFieldsresponse = { "success": true, "data": [ { "id": 12500, "key": "c4ecbe01c34994ede3a50c0f8", "name": "Lead Type", "options": [ { "label": "Expired", "id": 28 }, { "label": "FSBO", "id": 29 }, { "label": "FRBO", "id": 30 }, { "label": "Other", "id": 31 } ], "mandatory_flag": false } ] }; const res = dealFieldsresponse.data.reduce((ar, {key, options}) => { if (key == searchKey) { options.forEach(({id, label}) => { if (id == searchId) ar.push(label); }); } return ar; }, []); console.log(res)
當您想使用 multiple key
和id
檢索多個值時,以下示例腳本如何? 在此示例腳本中,將搜索密鑰c4ecbe01c34994ede3a50c0f8
和 id 28
以及密鑰a4269fb4730cf7fd1787752be94eacbc4b0de24e
和 id 37
並檢索label
的值。
const obj = { "c4ecbe01c34994ede3a50c0f8": {id: 28}, "a4269fb4730cf7fd1787752be94eacbc4b0de24e": {id: 37} }; // Please set the key and id you want to search. const dealFieldsresponse = { "success": true, "data": [ { "id": 12500, "key": "c4ecbe01c34994ede3a50c0f8", "name": "Lead Type", "options": [ { "label": "Expired", "id": 28 }, { "label": "FSBO", "id": 29 }, { "label": "FRBO", "id": 30 }, { "label": "Other", "id": 31 } ], "mandatory_flag": false } ] }; dealFieldsresponse.data.forEach(({key, options}) => { if (obj[key]) { options.forEach(({id, label}) => { if (id == obj[key].id) obj[key].label = label; }); } }); console.log(obj)
運行上述腳本后,得到如下結果。
{
"c4ecbe01c34994ede3a50c0f8":{"id":28,"label":"Expired"},
"a4269fb4730cf7fd1787752be94eacbc4b0de24e":{"id":37}
}
c4ecbe01c34994ede3a50c0f8
和 id 28
的標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.