[英]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.