[英]how can I pass another parameter to javascript map() function
I wanted to create generalized function from the solution given in the following link array of objects我想从以下对象链接数组中给出的解决方案创建通用 function
Say for example, my array looks like below例如,我的数组如下所示
ID TAGS
1 {"tags": [{"tag1": "a"}, {"tag1": "b"}, {"tag2":"123"}, {"tag2":"345"}]}
2 {"tags": [{"tag1": "c"}, {"tag1": "d"}, {"tag2":"789"}, {"tag2":"678"}]}
From the javascript UDF provided in the above link is working fine.从上述链接中提供的 javascript UDF 工作正常。 However I wanted to use that as generalized one like getting values of tag1 and tag2 by passing them as input parameter.
但是,我想将其用作通用方法,例如通过将它们作为输入参数传递来获取 tag1 和 tag2 的值。 Please find below the UDF which is working fine to get the tag1/tag2 by calling them directly.
请在 UDF 下方找到,它可以通过直接调用它们来正常获取 tag1/tag2。
create or replace function extract_tags(a array)
returns array
language javascript
strict
as '
return A.map(function(d) {return d.tag1});
';
SELECT ID, EXTRACT_TAGS(PAYLOAD:tags) AS tags from t1; -- This will give the result for tag1.
I don't want to recreate the same function for getting the tag2 value's.我不想重新创建相同的 function 来获取 tag2 值。 Instead wanted to pass the KEY as parameter and get the desired result.
而是想将 KEY 作为参数传递并获得所需的结果。 Any help on this is much appreciated.. something like below.. sorry if the below code is inappropriate.. I'm new to java and snowflake.
非常感谢您对此的任何帮助.. 如下所示.. 抱歉,如果以下代码不合适.. 我是 java 和雪花的新手。
create or replace function extract_tags(a array, b varchar)
returns array
language javascript
strict
as '
return A.map(function(d) {return d.{B}}); //Calling the second parameter to get the appropriate values
';
SELECT ID, EXTRACT_TAGS(PAYLOAD:tags, 'tag1') AS tags from t1; --To get the tag1 values
SELECT ID, EXTRACT_TAGS(PAYLOAD:tags, 'tag2') AS tags from t1; --Using the same function to get tag2 values
In return A.map(function(d) {return d.{B}});
作为
return A.map(function(d) {return d.{B}});
the map function should be return d[B]
instead. map function 应该
return d[B]
。
( copying @Pipetus answer in a comment to an answer, so this question can find closure as answered ) (在对答案的评论中复制@Pipetus 的答案,这样这个问题就可以找到答案了)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.