[英]Can I pass "this" as a parameter to another function in javascript
[英]how can I pass another parameter to javascript map() function
我想從以下對象鏈接數組中給出的解決方案創建通用 function
例如,我的數組如下所示
ID TAGS
1 {"tags": [{"tag1": "a"}, {"tag1": "b"}, {"tag2":"123"}, {"tag2":"345"}]}
2 {"tags": [{"tag1": "c"}, {"tag1": "d"}, {"tag2":"789"}, {"tag2":"678"}]}
從上述鏈接中提供的 javascript UDF 工作正常。 但是,我想將其用作通用方法,例如通過將它們作為輸入參數傳遞來獲取 tag1 和 tag2 的值。 請在 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.
我不想重新創建相同的 function 來獲取 tag2 值。 而是想將 KEY 作為參數傳遞並獲得所需的結果。 非常感謝您對此的任何幫助.. 如下所示.. 抱歉,如果以下代碼不合適.. 我是 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
作為return A.map(function(d) {return d.{B}});
map function 應該return d[B]
。
(在對答案的評論中復制@Pipetus 的答案,這樣這個問題就可以找到答案了)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.