简体   繁体   English

我怎样才能将这个 Javascript 字典键和值传递到 Function 中?

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

This Dictionary called dict has the Table Id Selectors as the Keys and "Hellos" as the Values.这个名为 dict 的字典将表 ID 选择器作为键,将“Hellos”作为值。

How can I pass this Dictionary into this function called applyDictionary that takes in the tableId key and value "Hellos" from the dictionary??如何将此字典传递到这个名为 applyDictionary 的 function 中,它从字典中获取 tableId 键和值“Hellos”?

This function doesn't take in the jQuery Selectors IDs as Parameters as in I cannot pass Dictionary Table Keys with the #.此 function 不接受 jQuery 选择器 ID 作为参数,因为我无法使用 # 传递字典表键。

For Example I cannot pass applyDictionary(#tbl, 'hello');例如我不能通过 applyDictionary(#tbl, 'hello');

Only applyDictionary(tbl, 'hello');只有 applyDictionary(tbl, 'hello');

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

Get the entries of the object first, and for each entry, apply the function with the id and the value, and then get rid of the first character of the id.先获取object的条目,对于每个条目,应用带有id和值的function,然后去掉id的第一个字符。

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

Explained:解释:

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
    )
  )

You could use Object.entries()您可以使用Object.entries()

Example:例子:

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 ));

See https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Object/entries请参阅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