从对象中获取与另一个值匹配的值。

huangapple go评论99阅读模式
英文:

get a value from an object matching another value

问题

你想要从这个 JSON 数组中查找一个特定的 "nome" 属性,然后返回对应的 "url" 属性值。你可以使用 JavaScript 的 filter 方法来实现这个目标。以下是一个示例代码:

  1. // 假设你已经有了这个 JSON 数组
  2. const jsonArray = [
  3. // 你的 JSON 数据...
  4. ];
  5. // 要查找的 "nome" 值
  6. const targetNome = "fullcalendar";
  7. // 使用 filter 方法查找匹配的对象
  8. const matchingObject = jsonArray.filter(item => item.nome === targetNome);
  9. // 如果找到匹配的对象,返回对应的 "url" 属性值
  10. if (matchingObject.length > 0) {
  11. const urlValue = matchingObject[0].url;
  12. console.log(urlValue); // 这里输出 "fullcalendar.php"
  13. } else {
  14. console.log("未找到匹配的对象。");
  15. }

这段代码会在 jsonArray 中查找与 targetNome 匹配的对象,如果找到了匹配的对象,就返回对应的 "url" 属性值。

英文:

I have a json that is like the following:

  1. [
  2. {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
  3. {"id":2,"nome":"form_wizard","url":"form_wizard.php","label":"Crea uno Smartform","icon":"fas fa-plus","data_attribute":"data-action=\"create\" data-step=\"0\" data-token=\"0\"","parent":"Smartform"},
  4. {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
  5. {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
  6. {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
  7. {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
  8. {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
  9. {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
  10. {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
  11. {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
  12. ]

I currently parse it with JSON.parse and use it efficiently. What I'd like to do now is the following.
I have a variable whose value is "fullcalendar" (can be any of the "nome" in the json). I want to look for that in the array and return "fullcalendar.php" that is the value of another property of this object.

How do I do it? I am trying to understand if I can do it with filter but don't see how to implement it. Any suggestion?

答案1

得分: 1

使用.find方法来返回包含值为fullcalendar的对象,然后仅打印url属性。

  1. const arr = [
  2. {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
  3. {"id":2,"nome":"form_wizard","url":"form_wizard.php","label":"Crea uno Smartform","icon":"fas fa-plus","data_attribute":"data-action=\"create\" data-step=\"0\" data-token=\"0\"","parent":"Smartform"},
  4. {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
  5. {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
  6. {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
  7. {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
  8. {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
  9. {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
  10. {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
  11. {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
  12. ];
  13. const result = arr.find(x => x.nome == "fullcalendar")
  14. console.log(result.url)

这段代码会找到名为fullcalendar的对象,并打印其url属性。

英文:

Use .find method to return the object with fullcalendar value in it then print only the url property

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const arr = [
  2. {&quot;id&quot;:1,&quot;nome&quot;:&quot;smartform&quot;,&quot;url&quot;:&quot;smartform.php&quot;,&quot;label&quot;:&quot;Dashboard&quot;,&quot;icon&quot;:&quot;fas fa-th-large&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  3. {&quot;id&quot;:2,&quot;nome&quot;:&quot;form_wizard&quot;,&quot;url&quot;:&quot;form_wizard.php&quot;,&quot;label&quot;:&quot;Crea uno Smartform&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;data-action=\&quot;create\&quot; data-step=\&quot;0\&quot; data-token=\&quot;0\&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  4. {&quot;id&quot;:3,&quot;nome&quot;:&quot;fullcalendar&quot;,&quot;url&quot;:&quot;fullcalendar.php&quot;,&quot;label&quot;:&quot;Calendario&quot;,&quot;icon&quot;:&quot;far fa-calendar&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  5. {&quot;id&quot;:4,&quot;nome&quot;:&quot;gantt&quot;,&quot;url&quot;:&quot;gantt.php&quot;,&quot;label&quot;:&quot;Gantt&quot;,&quot;icon&quot;:&quot;fas fa-stream&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  6. {&quot;id&quot;:5,&quot;nome&quot;:&quot;timesheet&quot;,&quot;url&quot;:&quot;timesheet.php&quot;,&quot;label&quot;:&quot;Timesheet&quot;,&quot;icon&quot;:&quot;fas fa-hourglass&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  7. {&quot;id&quot;:6,&quot;nome&quot;:&quot;kanban&quot;,&quot;url&quot;:&quot;kanban.php&quot;,&quot;label&quot;:&quot;Kanban&quot;,&quot;icon&quot;:&quot;fas fa-list-ul&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  8. {&quot;id&quot;:7,&quot;nome&quot;:&quot;openpoints&quot;,&quot;url&quot;:&quot;items.php?tipo=openpoints&quot;,&quot;label&quot;:&quot;Open Points&quot;,&quot;icon&quot;:&quot;fas fa-keyboard&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  9. {&quot;id&quot;:8,&quot;nome&quot;:&quot;risks&quot;,&quot;url&quot;:&quot;items.php?tipo=risks&quot;,&quot;label&quot;:&quot;Rischi&quot;,&quot;icon&quot;:&quot;fas fa-exclamation&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  10. {&quot;id&quot;:9,&quot;nome&quot;:&quot;issues&quot;,&quot;url&quot;:&quot;items.php?tipo=issues&quot;,&quot;label&quot;:&quot;Issue&quot;,&quot;icon&quot;:&quot;fas fa-fire&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  11. {&quot;id&quot;:10,&quot;nome&quot;:&quot;changerequests&quot;,&quot;url&quot;:&quot;items.php?tipo=changerequests&quot;,&quot;label&quot;:&quot;Change Requests&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;}
  12. ];
  13. const result = arr.find(x =&gt; x.nome == &quot;fullcalendar&quot;)
  14. console.log(result.url)

<!-- end snippet -->

答案2

得分: 1

你可以使用find()方法而不是filter()来查找具有特定值的对象,该值位于你的JSON数组的"nome"属性中。这里是一个示例:

  1. const jsonArray = [
  2. // 在这里放入你的JSON数组...
  3. ];
  4. const searchTerm = "fullcalendar";
  5. const foundObject = jsonArray.find(obj => obj.nome === searchTerm);
  6. if (foundObject) {
  7. const url = foundObject.url;
  8. console.log(url); // "fullcalendar.php"
  9. } else {
  10. console.log("Object not found in the array.");
  11. }

在这个示例中,find()方法用于搜索jsonArray中第一个具有"nome"属性等于searchTerm的对象。如果找到匹配的对象,则返回其"url"属性的值。否则,会记录一条指示未找到对象的消息。

在这种情况下,使用find()filter()更高效,因为它在找到第一个匹配对象后立即停止搜索,而不是遍历整个数组。

英文:

You can use the find() method instead of filter() to find the object with a specific value for the "nome" property in your JSON array. Here's an example:

  1. const jsonArray = [
  2. // Your JSON array here...
  3. ];
  4. const searchTerm = &quot;fullcalendar&quot;;
  5. const foundObject = jsonArray.find(obj =&gt; obj.nome === searchTerm);
  6. if (foundObject) {
  7. const url = foundObject.url;
  8. console.log(url); // &quot;fullcalendar.php&quot;
  9. } else {
  10. console.log(&quot;Object not found in the array.&quot;);
  11. }

In this example, the find() method is used to search for the first object in the jsonArray that has a "nome" property equal to the searchTerm. If a matching object is found, the value of its "url" property is returned. Otherwise, a message indicating that the object was not found is logged.

Using find() is a more efficient choice than filter() in this case because it stops searching as soon as the first matching object is found, rather than iterating over the entire array.

答案3

得分: 0

你需要使用Array.prototype.find

  1. const items = [
  2. {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
  3. {"id":2,"nome":"form_wizard","url":"form_wizard.php","label":"Crea uno Smartform","icon":"fas fa-plus","data_attribute":"data-action=\"create\" data-step=\"0\" data-token=\"0\"","parent":"Smartform"},
  4. {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
  5. {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
  6. {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
  7. {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
  8. {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
  9. {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
  10. {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
  11. {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
  12. ];
  13. function getUrl(nome) {
  14. return items.find(item => item.nome === nome)?.url;
  15. }
  16. // 更灵活的选项
  17. function getProperty(searchKey, searchValue, returnKey) {
  18. const item = items.find(item => item[searchKey] === searchValue);
  19. return item ? item[returnKey] : item;
  20. }
  21. console.log(getUrl('fullcalendar'));
  22. console.log(getProperty('nome', 'fullcalendar', 'url'));
英文:

You need to use Array.prototype.find:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const items = [
  2. {&quot;id&quot;:1,&quot;nome&quot;:&quot;smartform&quot;,&quot;url&quot;:&quot;smartform.php&quot;,&quot;label&quot;:&quot;Dashboard&quot;,&quot;icon&quot;:&quot;fas fa-th-large&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  3. {&quot;id&quot;:2,&quot;nome&quot;:&quot;form_wizard&quot;,&quot;url&quot;:&quot;form_wizard.php&quot;,&quot;label&quot;:&quot;Crea uno Smartform&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;data-action=\&quot;create\&quot; data-step=\&quot;0\&quot; data-token=\&quot;0\&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  4. {&quot;id&quot;:3,&quot;nome&quot;:&quot;fullcalendar&quot;,&quot;url&quot;:&quot;fullcalendar.php&quot;,&quot;label&quot;:&quot;Calendario&quot;,&quot;icon&quot;:&quot;far fa-calendar&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  5. {&quot;id&quot;:4,&quot;nome&quot;:&quot;gantt&quot;,&quot;url&quot;:&quot;gantt.php&quot;,&quot;label&quot;:&quot;Gantt&quot;,&quot;icon&quot;:&quot;fas fa-stream&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  6. {&quot;id&quot;:5,&quot;nome&quot;:&quot;timesheet&quot;,&quot;url&quot;:&quot;timesheet.php&quot;,&quot;label&quot;:&quot;Timesheet&quot;,&quot;icon&quot;:&quot;fas fa-hourglass&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  7. {&quot;id&quot;:6,&quot;nome&quot;:&quot;kanban&quot;,&quot;url&quot;:&quot;kanban.php&quot;,&quot;label&quot;:&quot;Kanban&quot;,&quot;icon&quot;:&quot;fas fa-list-ul&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  8. {&quot;id&quot;:7,&quot;nome&quot;:&quot;openpoints&quot;,&quot;url&quot;:&quot;items.php?tipo=openpoints&quot;,&quot;label&quot;:&quot;Open Points&quot;,&quot;icon&quot;:&quot;fas fa-keyboard&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  9. {&quot;id&quot;:8,&quot;nome&quot;:&quot;risks&quot;,&quot;url&quot;:&quot;items.php?tipo=risks&quot;,&quot;label&quot;:&quot;Rischi&quot;,&quot;icon&quot;:&quot;fas fa-exclamation&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  10. {&quot;id&quot;:9,&quot;nome&quot;:&quot;issues&quot;,&quot;url&quot;:&quot;items.php?tipo=issues&quot;,&quot;label&quot;:&quot;Issue&quot;,&quot;icon&quot;:&quot;fas fa-fire&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  11. {&quot;id&quot;:10,&quot;nome&quot;:&quot;changerequests&quot;,&quot;url&quot;:&quot;items.php?tipo=changerequests&quot;,&quot;label&quot;:&quot;Change Requests&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;}
  12. ];
  13. function getUrl(nome) {
  14. return items.find(item =&gt; item.nome === nome)?.url;
  15. }
  16. // more flexible option
  17. function getProperty(searchKey, searchValue, returnKey) {
  18. const item = items.find(item =&gt; item[searchKey] === searchValue);
  19. return item ? item[returnKey] : item;
  20. }
  21. console.log(getUrl(&#39;fullcalendar&#39;));
  22. console.log(getProperty(&#39;nome&#39;, &#39;fullcalendar&#39;, &#39;url&#39;));

<!-- end snippet -->

答案4

得分: 0

你可以使用find(或filter)来访问单个项,但每次都需要遍历数组。听起来你想要通过字符串键对项进行任意访问,这本质上是一个映射/字典。在这种情况下,我建议将项的数组转换为以“nome”值为键的相同项的映射。然后你可以直接访问它,效率更高,因为对键的随机访问是常数时间的。假设你在页面/应用程序的生命周期中不断访问不同的值,这样会更好地工作。

  1. const items = [
  2. {
  3. "id": 1,
  4. "nome": "smartform",
  5. "url": "smartform.php",
  6. "label": "Dashboard",
  7. "icon": "fas fa-th-large",
  8. "data_attribute": "",
  9. "parent": "Smartform"
  10. },
  11. // ... 其他项
  12. ];
  13. const itemsMap = items.reduce((acc, item) => {
  14. // 如果有'nome'属性,将其添加到映射中
  15. if (item.hasOwnProperty('nome')) {
  16. acc[item.nome] = item;
  17. }
  18. return acc;
  19. }, {});
  20. console.log(itemsMap.fullcalendar?.url);

在上面的代码中,我们将原始数组items转换为一个映射itemsMap,然后可以直接访问fullcalendar的URL属性。

英文:

While you could access an individual item using find (or filter), it requires iterating over the array each time. It sounds like you want arbitrary access to an item by a string key, which is essentially a map/dict. In this case, I would suggest converting from an array of items to a map of the same items keyed by the 'nome' value. Then you could access it directly, and more efficiently since random access to a key is constant time. This would work better assuming you are accessing different values at different time throughout the life of your page/application.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const items = [{
  2. &quot;id&quot;: 1,
  3. &quot;nome&quot;: &quot;smartform&quot;,
  4. &quot;url&quot;: &quot;smartform.php&quot;,
  5. &quot;label&quot;: &quot;Dashboard&quot;,
  6. &quot;icon&quot;: &quot;fas fa-th-large&quot;,
  7. &quot;data_attribute&quot;: &quot;&quot;,
  8. &quot;parent&quot;: &quot;Smartform&quot;
  9. },
  10. {
  11. &quot;id&quot;: 2,
  12. &quot;nome&quot;: &quot;form_wizard&quot;,
  13. &quot;url&quot;: &quot;form_wizard.php&quot;,
  14. &quot;label&quot;: &quot;Crea uno Smartform&quot;,
  15. &quot;icon&quot;: &quot;fas fa-plus&quot;,
  16. &quot;data_attribute&quot;: &quot;data-action=\&quot;create\&quot; data-step=\&quot;0\&quot; data-token=\&quot;0\&quot;&quot;,
  17. &quot;parent&quot;: &quot;Smartform&quot;
  18. },
  19. {
  20. &quot;id&quot;: 3,
  21. &quot;nome&quot;: &quot;fullcalendar&quot;,
  22. &quot;url&quot;: &quot;fullcalendar.php&quot;,
  23. &quot;label&quot;: &quot;Calendario&quot;,
  24. &quot;icon&quot;: &quot;far fa-calendar&quot;,
  25. &quot;data_attribute&quot;: &quot;&quot;,
  26. &quot;parent&quot;: &quot;Tools&quot;
  27. },
  28. {
  29. &quot;id&quot;: 4,
  30. &quot;nome&quot;: &quot;gantt&quot;,
  31. &quot;url&quot;: &quot;gantt.php&quot;,
  32. &quot;label&quot;: &quot;Gantt&quot;,
  33. &quot;icon&quot;: &quot;fas fa-stream&quot;,
  34. &quot;data_attribute&quot;: &quot;&quot;,
  35. &quot;parent&quot;: &quot;Tools&quot;
  36. },
  37. {
  38. &quot;id&quot;: 5,
  39. &quot;nome&quot;: &quot;timesheet&quot;,
  40. &quot;url&quot;: &quot;timesheet.php&quot;,
  41. &quot;label&quot;: &quot;Timesheet&quot;,
  42. &quot;icon&quot;: &quot;fas fa-hourglass&quot;,
  43. &quot;data_attribute&quot;: &quot;&quot;,
  44. &quot;parent&quot;: &quot;Tools&quot;
  45. },
  46. {
  47. &quot;id&quot;: 6,
  48. &quot;nome&quot;: &quot;kanban&quot;,
  49. &quot;url&quot;: &quot;kanban.php&quot;,
  50. &quot;label&quot;: &quot;Kanban&quot;,
  51. &quot;icon&quot;: &quot;fas fa-list-ul&quot;,
  52. &quot;data_attribute&quot;: &quot;&quot;,
  53. &quot;parent&quot;: &quot;Tools&quot;
  54. },
  55. {
  56. &quot;id&quot;: 7,
  57. &quot;nome&quot;: &quot;openpoints&quot;,
  58. &quot;url&quot;: &quot;items.php?tipo=openpoints&quot;,
  59. &quot;label&quot;: &quot;Open Points&quot;,
  60. &quot;icon&quot;: &quot;fas fa-keyboard&quot;,
  61. &quot;data_attribute&quot;: &quot;&quot;,
  62. &quot;parent&quot;: &quot;Risk Management&quot;
  63. },
  64. {
  65. &quot;id&quot;: 8,
  66. &quot;nome&quot;: &quot;risks&quot;,
  67. &quot;url&quot;: &quot;items.php?tipo=risks&quot;,
  68. &quot;label&quot;: &quot;Rischi&quot;,
  69. &quot;icon&quot;: &quot;fas fa-exclamation&quot;,
  70. &quot;data_attribute&quot;: &quot;&quot;,
  71. &quot;parent&quot;: &quot;Risk Management&quot;
  72. },
  73. {
  74. &quot;id&quot;: 9,
  75. &quot;nome&quot;: &quot;issues&quot;,
  76. &quot;url&quot;: &quot;items.php?tipo=issues&quot;,
  77. &quot;label&quot;: &quot;Issue&quot;,
  78. &quot;icon&quot;: &quot;fas fa-fire&quot;,
  79. &quot;data_attribute&quot;: &quot;&quot;,
  80. &quot;parent&quot;: &quot;Risk Management&quot;
  81. },
  82. {
  83. &quot;id&quot;: 10,
  84. &quot;nome&quot;: &quot;changerequests&quot;,
  85. &quot;url&quot;: &quot;items.php?tipo=changerequests&quot;,
  86. &quot;label&quot;: &quot;Change Requests&quot;,
  87. &quot;icon&quot;: &quot;fas fa-plus&quot;,
  88. &quot;data_attribute&quot;: &quot;&quot;,
  89. &quot;parent&quot;: &quot;Risk Management&quot;
  90. }
  91. ]
  92. const itemsMap = items.reduce((acc, item) =&gt; {
  93. // add it to the map if it has a nome
  94. if (item.hasOwnProperty(&#39;nome&#39;)) {
  95. acc[item.nome] = item;
  96. }
  97. return acc;
  98. }, {})
  99. console.log(itemsMap.fullcalendar ? .url)

<!-- end snippet -->

https://playcode.io/1482928

答案5

得分: -1

  1. [
  2. {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
  3. {"id":2,"nome":"form_wizard","url":"form_wizard.php","label":"Crea uno Smartform","icon":"fas fa-plus","data_attribute":"data-action=\"create\" data-step=\"0\" data-token=\"0\"","parent":"Smartform"},
  4. {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
  5. {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
  6. {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
  7. {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
  8. {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
  9. {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
  10. {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
  11. {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
  12. ].filter(ele => ele.nome === "fullcalendar")[0].url
  1. filter
    可以使用filter()函数来获取符合您提供的选项的元素。您可以在此文档中了解更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

  2. [0]
    如果您想要从中获取一个项目,您可以通过索引切片来获取第一个项目。但是您需要处理过滤后的数组长度为0的情况,然后[0]可能会是未定义的。

  3. .url
    您可以通过.来访问属性。

英文:
  1. [
  2. {&quot;id&quot;:1,&quot;nome&quot;:&quot;smartform&quot;,&quot;url&quot;:&quot;smartform.php&quot;,&quot;label&quot;:&quot;Dashboard&quot;,&quot;icon&quot;:&quot;fas fa-th-large&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  3. {&quot;id&quot;:2,&quot;nome&quot;:&quot;form_wizard&quot;,&quot;url&quot;:&quot;form_wizard.php&quot;,&quot;label&quot;:&quot;Crea uno Smartform&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;data-action=\&quot;create\&quot; data-step=\&quot;0\&quot; data-token=\&quot;0\&quot;&quot;,&quot;parent&quot;:&quot;Smartform&quot;},
  4. {&quot;id&quot;:3,&quot;nome&quot;:&quot;fullcalendar&quot;,&quot;url&quot;:&quot;fullcalendar.php&quot;,&quot;label&quot;:&quot;Calendario&quot;,&quot;icon&quot;:&quot;far fa-calendar&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  5. {&quot;id&quot;:4,&quot;nome&quot;:&quot;gantt&quot;,&quot;url&quot;:&quot;gantt.php&quot;,&quot;label&quot;:&quot;Gantt&quot;,&quot;icon&quot;:&quot;fas fa-stream&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  6. {&quot;id&quot;:5,&quot;nome&quot;:&quot;timesheet&quot;,&quot;url&quot;:&quot;timesheet.php&quot;,&quot;label&quot;:&quot;Timesheet&quot;,&quot;icon&quot;:&quot;fas fa-hourglass&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  7. {&quot;id&quot;:6,&quot;nome&quot;:&quot;kanban&quot;,&quot;url&quot;:&quot;kanban.php&quot;,&quot;label&quot;:&quot;Kanban&quot;,&quot;icon&quot;:&quot;fas fa-list-ul&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Tools&quot;},
  8. {&quot;id&quot;:7,&quot;nome&quot;:&quot;openpoints&quot;,&quot;url&quot;:&quot;items.php?tipo=openpoints&quot;,&quot;label&quot;:&quot;Open Points&quot;,&quot;icon&quot;:&quot;fas fa-keyboard&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  9. {&quot;id&quot;:8,&quot;nome&quot;:&quot;risks&quot;,&quot;url&quot;:&quot;items.php?tipo=risks&quot;,&quot;label&quot;:&quot;Rischi&quot;,&quot;icon&quot;:&quot;fas fa-exclamation&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  10. {&quot;id&quot;:9,&quot;nome&quot;:&quot;issues&quot;,&quot;url&quot;:&quot;items.php?tipo=issues&quot;,&quot;label&quot;:&quot;Issue&quot;,&quot;icon&quot;:&quot;fas fa-fire&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;},
  11. {&quot;id&quot;:10,&quot;nome&quot;:&quot;changerequests&quot;,&quot;url&quot;:&quot;items.php?tipo=changerequests&quot;,&quot;label&quot;:&quot;Change Requests&quot;,&quot;icon&quot;:&quot;fas fa-plus&quot;,&quot;data_attribute&quot;:&quot;&quot;,&quot;parent&quot;:&quot;Risk Management&quot;}
  12. ].filter(ele =&gt; ele.nome === &quot;fullcalendar&quot;)[0].url
  1. filter
    you can get elements that fits your option you provided in filter() function. you can get more in this docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

  2. [0]
    If you want one item from it, you should get first item by index slicing. But you need to handle the case that filtered array's length is 0. then [0] might be undefined.

  3. .url
    you can access to property by ..

huangapple
  • 本文由 发表于 2023年5月25日 00:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定