简体   繁体   中英

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.

How can I pass this Dictionary into this function called applyDictionary that takes in the tableId key and value "Hellos" from the dictionary??

This function doesn't take in the jQuery Selectors IDs as Parameters as in I cannot pass Dictionary Table Keys with the #.

For Example I cannot pass applyDictionary(#tbl, 'hello');

Only 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.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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM