繁体   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