簡體   English   中英

我怎樣才能將這個 Javascript 字典鍵和值傳遞到 Function 中?

[英]How can i pass this Javascript Dictionary Key and Value Into A Function?

這個名為 dict 的字典將表 ID 選擇器作為鍵,將“Hellos”作為值。

如何將此字典傳遞到這個名為 applyDictionary 的 function 中,它從字典中獲取 tableId 鍵和值“Hellos”?

此 function 不接受 jQuery 選擇器 ID 作為參數,因為我無法使用 # 傳遞字典表鍵。

例如我不能通過 applyDictionary(#tbl, 'hello');

只有 applyDictionary(tbl, 'hello');

var dict = {
  '#tbl': 'hello',
  '#tbl1': 'hello1',
  '#tbl2': 'hello2',
  '#tbl3': 'hello3',
};
applyDictionary(table_ID, value);

先獲取object的條目,對於每個條目,應用帶有id和值的function,然后去掉id的第一個字符。

Object.entries(dict).map(([id, value]) => applyDictionary(id.slice(1), value))

解釋:

Object.entries(dict) // Get entries of the object as 2-items arrays with the key and the value
  .map(([id, value]) => // destructure every item
    applyDictionary(
      id.slice(1), // Get rid of the first character and pass it as first argument to applyDictonary
      value, // Pass the value as 2nd argument
    )
  )

您可以使用Object.entries()

例子:

var tblnsnsearchresults_tooltips  = {
    '#tblnsnsearchresults_uoicd': 'hello',
    '#tblnsnsearchresults_nsnid': 'hello1',
    '#tblnsnsearchresults_fsccd': 'hello2',
    '#tblnsnsearchresults_nsndescription': 'hello3',
};

let applycolumnheadertooltips = entries =>
    entries.forEach(([gridId, tooltips])=>console.log(`${gridId} => ${tooltips}`));

    applycolumnheadertooltips(Object.entries(tblnsnsearchresults_tooltips ));

請參閱https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Object/entries

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM