使用循环for每个Google Apps脚本Javascript查找数组中的值 [英] Look up values in an array using looping forEach Google Apps Script Javascript
问题描述
我有一个看起来像以下{key:id numbers}
I have an object that looks like the following {key: id numbers}
var obj = {
"c4ecb": {id: [3]},
"a4269": {id: [34,36]},
"d76fa": {id: [54,55,60,61]},
"58cb5": {id: [67]}
}
我如何在以下数组中的每个id
上循环,并返回label
?
How do I loop each above id
in the following array, and return the label
?
var response =
{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}
最后,我想做的是查找键并返回一个ID值字符串.
例如,输入c4ecb
和输出strawberry
.输入a4269
和输出lettuce, radish
.输入d76fa
并输出"spaghetti, rigatoni, lasagna, fettuccine
"
Finally, what I want to do is look up the key and return a string of id values.
For example, input c4ecb
and output strawberry
. Input a4269
and output lettuce, radish
. Input d76fa
and output "spaghetti, rigatoni, lasagna, fettuccine
"
我认为要将多个标签输出合并为一个字符串,我可以使用类似的
I think to join the multiple labels output into one string I could use something like
array.data.vegetables.map(vegetables => vegetables.value).join(', ')].toString();
所以最终我想拥有类似的东西
So in the end I want to have something like
var fruits = [some code that outputs "strawberry"];
var vegetables = [some code that outputs "lettuce, radish"];
var pasta = [some code that outputs "spaghetti, rigatoni, lasagna, fettuccine"];
到目前为止我尝试过的是:
仅当要调用一个id
时,以下循环才会返回id
:例如仅在其中{id: 3}
但在{id: 34,36}
之类的情况下返回null的情况下(因为它正在id
中查找'34,36
',该值不存在-我需要逐一查找.
What I've tried so far:
The following loop will return the id
only if there is one id
to be called for: e.g. only in case one where {id: 3}
but returns null in cases like {id: 34,36}
(because it's looking for '34,36
' in id
, which doesn't exist - I need to look for each one individually.
response.data.forEach(({key, options}) => {
if (obj[key]) {
options.forEach(({id, label}) => {
if (id == obj[key].id) obj[key].label = label;
});
}
});
console.log(obj)
推荐答案
过滤response
对象以将其重点放在与id
相匹配的类别上.
映射到options
数组,然后选择出现在obj[id]
中的项目.
最后将过滤的结果转换为字符串.
Filter the response
object to focus on the category that matches the id
.
Map over the options
array and select the items which appear in obj[id]
.
Finally convert the filtered results to a string.
有关实现,请参见下面的filteredLabelsAsString()
函数.
See filteredLabelsAsString()
function below for implementation.
var obj = {
"c4ecb": {"id": [3]},
"a4269": {"id": [34,36]},
"d76fa": {"id": [54,55,60,61]},
"58cb5": {"id": [67]}
}
var response =
[{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}];
function filteredLabelsAsString(obj_key, obj, content=response) {
// sanity check: obj must contain obj_key
if (Object.keys(obj).includes(obj_key)) {
return content.filter((item) => {
// filter content using value of obj_key
return item.data[0].key == obj_key;
}).map((item) => {
// item : { success: true, data: [] }
// map over options array
return item.data[0].options.map((opt) => {
// option : {id, label}
// return the label if the id is in the obj object's list
if (obj[item.data[0].key].id.includes(opt.id))
return opt.label;
}).filter((label) => {
// filter out empty items
return label !== undefined;
});
}).join(",");
}
// if obj does not contain obj_key return empty string
return "";
}
console.log("fruits: " + filteredLabelsAsString("c4ecb", obj));
console.log("vegetables: " + filteredLabelsAsString("a4269", obj));
console.log("pasta: " + filteredLabelsAsString("d76fa", obj));
这篇关于使用循环for每个Google Apps脚本Javascript查找数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!