【问题标题】:How can i pass this Javascript Dictionary Key and Value Into A Function?如何将此 Javascript 字典键和值传递给函数?
【发布时间】:2021-01-01 19:04:08
【问题描述】:

这个名为 dict 的字典将表 ID 选择器作为键,将“Hellos”作为值。

如何将此字典传递给这个名为 applyDictionary 的函数,该函数从字典中获取 tableId 键和值“Hellos”??

此函数不接受 jQuery 选择器 ID 作为参数,因为我无法使用 # 传递字典表键。

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

只适用字典(tbl, 'hello');

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

【问题讨论】:

  • 很难理解你想要完成什么; applyDictionary 的期望输出是什么?
  • 你可以试试 applyDictionary('#tbl', 'hello');

标签: javascript jquery dictionary


【解决方案1】:

先获取对象的条目,对于每个条目,应用带有id和值的函数,然后去掉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
    )
  )

【讨论】:

    【解决方案2】:

    你可以使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      相关资源
      最近更新 更多