简体   繁体   English

如何将另一个参数传递给 javascript map() function

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

相关问题 我可以将“this”作为参数传递给javascript中的另一个函数吗 - Can I pass "this" as a parameter to another function in javascript 如何传递额外的参数以在地图Javascript中起作用 - How to pass an extra parameter to function in map Javascript javascript—如何调用同时具有事件对象和另一个参数的函数? - javascript— how can I invoke a function that has both an event object and a another parameter to pass in? 如何在javascript中使用函数参数成功传递响应数据? - How can I pass the response data successfully with function parameter in javascript? 在javascript中,如何传递带有参数的函数以供以后调用? - In javascript, how can I pass a function with parameter to be called later? 如何将 JavaScript ES6 箭头函数作为参数传递 - How can I Pass a JavaScript ES6 Arrow Function as a Parameter 如何将Smarty的参数传递给javascript函数? - How can I pass a parameter of Smarty to a javascript function? 如何将 function 传递到 JavaScript 中的另一个 function - how can I pass a function into another function in JavaScript 我可以将此函数传递给Javascript中的另一个函数吗? - Can I pass this function to another function in Javascript? 如何在JavaScript中将函数作为参数传递 - How do I pass function as a parameter in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM