[英]Look up value from array in another array, and then match a second value Google Apps Script Javascript JSON API
I'm importing data from my crm Pipedrive into Google sheets using Google Apps Script.我正在使用 Google Apps Script 将数据从我的 crm Pipedrive 导入 Google 表格。 This is part of a larger process but I'm at an impasse with this section of the script.这是更大过程的一部分,但我对脚本的这一部分陷入僵局。 I need to return a value by matching two parts of one array to another array.我需要通过将一个数组的两个部分与另一个数组进行匹配来返回一个值。
First I pull all deal fields, which returns custom field keys and their id/label pairs.首先,我提取所有交易字段,它返回自定义字段键及其 ID/标签对。 Here's a simplified output example:这是一个简化的输出示例:
{
"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
}
]
}
Then I have separate info from a specific deal that includes an id.然后我从包含 id 的特定交易中获得单独的信息。 I need to match the below id 28
to the above array and return the label Expired
: var leadType = dealresponse.data["c4ecbe01c34994ede3a50c0f8"];
我需要将下面的 id 28
与上面的数组匹配并返回标签Expired
: var leadType = dealresponse.data["c4ecbe01c34994ede3a50c0f8"];
which returns 28
I don't know what '28' means so that's why I need to match it to the label Expired.它返回28
我不知道 '28' 是什么意思,所以这就是为什么我需要将它与标签 Expired 匹配。 The dealFields array is long, maybe 50 or 100 of the above array objects. dealFields 数组很长,可能是上述数组对象的 50 或 100 个。 And there are around 10 custom deal field keys where I will have to return the label base on matching the key and id.大约有 10 个自定义交易字段键,我将不得不根据匹配键和 ID 返回标签。 I think I have to loop each key and id to return the label.我想我必须循环每个键和 id 才能返回标签。 But not sure of the optimum way to do this and save on processing power.但不确定执行此操作并节省处理能力的最佳方法。
I tried:我试过:
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);
}
}
}
}
It's not working.它不起作用。 I'm new at javascript and programming in general so this is my best guess and I appreciate any insights.我是 javascript 和编程的新手,所以这是我最好的猜测,我很感激任何见解。
Edit: here's a bigger chunk of code that I have to work with:编辑:这是我必须使用的更大的代码块:
// 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"];
And the dealFieldsresponse
variable stores an array with many objects containing arrays. dealFieldsresponse
变量存储一个数组,其中包含许多包含数组的对象。 Here are two primary objects, as you can see each has a key
and then options
.这是两个主要对象,您可以看到每个对象都有一个key
和options
。 I need to match the key and then find the id
within options for each key
我需要匹配密钥,然后在每个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
},
Edit 2: how do I return the labels for multiple ids?编辑 2:如何返回多个 ID 的标签?
const obj = {
"a4269fb4730cf7fd1787752be94eacbc4b0de24e": {id: 37,38}, "58cb55090b55652b7f89a8b44074682d874c548a": {id: 44,45},
"2ec54cce0d091b69b1fd1a245c7aad02b57cadb8": {id: 126},
"fab84c732295022ecd7bdf58892a62cb4d8ecf24": {id: 50,52,54},
};
For example, I'd want the first to return red, blue
as a string, and the second to return green, orange
as a string.例如,我希望第一个返回red, blue
作为字符串,第二个返回green, orange
作为字符串。 Assuming the labels that match the id
s are colors.假设与id
匹配的标签是颜色。 The third one only has one id
, but the fourth one has three.第三个只有一个id
,但第四个有三个。 How do I account for this?我该如何解释? And I'd like my output to be some kind of array where I can then say search key
a4269fb4730cf7fd1787752be94eacbc4b0de24e
and return value red, blue
as a string我希望我的输出是某种数组,然后我可以说搜索key
a4269fb4730cf7fd1787752be94eacbc4b0de24e
并返回值red, blue
作为字符串
I believe your goal as follows.我相信你的目标如下。
label
using key
and id
from the JSON object in your question using Google Apps Script.您想使用 Google Apps 脚本从问题中的 JSON 对象中使用key
和id
检索label
的值。
"label": "Expired"
using "key": "c4ecbe01c34994ede3a50c0f8"
and "id": 28
.作为示例情况,您希望使用"key": "c4ecbe01c34994ede3a50c0f8"
和"id": 28
检索"label": "Expired"
的值。data
and options
. JSON 对象具有data
和options
数组。 Both arrays have the several elements.两个数组都有几个元素。dealFieldsresponse
is the JSON object in your question, dealFieldsresponse.data
and dealFieldsresponse.data[].options
are 1 dimensional array.如果dealFieldsresponse
是您问题中的 JSON 对象,则dealFieldsresponse.data
和dealFieldsresponse.data[].options
是一维数组。 When you want to retrieve the value of key
and id
, it is required to loop those arrays.当您要检索key
和id
的值时,需要循环这些数组。When above points are reflected to your script, it becomes as follows.当以上几点反映到你的脚本中时,它变成如下。
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); } } } }
As other sample script, how about the following script?作为其他示例脚本,下面的脚本怎么样? In this sample, the result values are put in an array.在此示例中,结果值放在一个数组中。
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)
When you want to retrieve the multiple values using the multiple key
and id
, how about the following sample script?当您想使用 multiple key
和id
检索多个值时,以下示例脚本如何? In this sample script, the key c4ecbe01c34994ede3a50c0f8
and id 28
and the key a4269fb4730cf7fd1787752be94eacbc4b0de24e
and id 37
are searched and the values of label
are retrieved.在此示例脚本中,将搜索密钥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)
When above script is run, the following result is obtained.运行上述脚本后,得到如下结果。
{
"c4ecbe01c34994ede3a50c0f8":{"id":28,"label":"Expired"},
"a4269fb4730cf7fd1787752be94eacbc4b0de24e":{"id":37}
}
c4ecbe01c34994ede3a50c0f8
and id 28
is retrieved.在上面的示例 JSON 对象中,检索了密钥c4ecbe01c34994ede3a50c0f8
和 id 28
的标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.