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

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

get a value from an object matching another value

问题

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

// 假设你已经有了这个 JSON 数组
const jsonArray = [
    // 你的 JSON 数据...
];

// 要查找的 "nome" 值
const targetNome = "fullcalendar";

// 使用 filter 方法查找匹配的对象
const matchingObject = jsonArray.filter(item => item.nome === targetNome);

// 如果找到匹配的对象,返回对应的 "url" 属性值
if (matchingObject.length > 0) {
    const urlValue = matchingObject[0].url;
    console.log(urlValue); // 这里输出 "fullcalendar.php"
} else {
    console.log("未找到匹配的对象。");
}

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

英文:

I have a json that is like the following:

[
    {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
    {"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"},
    {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
    {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
    {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
    {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
    {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
    {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
    {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
    {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
]

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属性。

const arr = [
    {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
    {"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"},
    {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
    {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
    {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
    {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
    {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
    {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
    {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
    {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
];

const result = arr.find(x => x.nome == "fullcalendar")
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 -->

const arr = [
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;}
];
const result = arr.find(x =&gt; x.nome == &quot;fullcalendar&quot;)
console.log(result.url)

<!-- end snippet -->

答案2

得分: 1

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

const jsonArray = [
    // 在这里放入你的JSON数组...
];

const searchTerm = "fullcalendar";
const foundObject = jsonArray.find(obj => obj.nome === searchTerm);

if (foundObject) {
    const url = foundObject.url;
    console.log(url); // "fullcalendar.php"
} else {
    console.log("Object not found in the array.");
}

在这个示例中,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:

const jsonArray = [
// Your JSON array here...
];
const searchTerm = &quot;fullcalendar&quot;;
const foundObject = jsonArray.find(obj =&gt; obj.nome === searchTerm);
if (foundObject) {
const url = foundObject.url;
console.log(url); // &quot;fullcalendar.php&quot;
} else {
console.log(&quot;Object not found in the array.&quot;);
}

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

const items = [
    {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
    {"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"},
    {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
    {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
    {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
    {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
    {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
    {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
    {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
    {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
];

function getUrl(nome) {
  return items.find(item => item.nome === nome)?.url;
}

// 更灵活的选项
function getProperty(searchKey, searchValue, returnKey) {
  const item = items.find(item => item[searchKey] === searchValue);
  return item ? item[returnKey] : item;
}

console.log(getUrl('fullcalendar'));
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 -->

const items = [
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;},
{&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;}
];
function getUrl(nome) {
return items.find(item =&gt; item.nome === nome)?.url;
}
// more flexible option
function getProperty(searchKey, searchValue, returnKey) {
const item = items.find(item =&gt; item[searchKey] === searchValue);
return item ? item[returnKey] : item;
}
console.log(getUrl(&#39;fullcalendar&#39;));
console.log(getProperty(&#39;nome&#39;, &#39;fullcalendar&#39;, &#39;url&#39;));

<!-- end snippet -->

答案4

得分: 0

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

const items = [
  {
    "id": 1,
    "nome": "smartform",
    "url": "smartform.php",
    "label": "Dashboard",
    "icon": "fas fa-th-large",
    "data_attribute": "",
    "parent": "Smartform"
  },
  // ... 其他项
];

const itemsMap = items.reduce((acc, item) => {
  // 如果有'nome'属性,将其添加到映射中
  if (item.hasOwnProperty('nome')) {
    acc[item.nome] = item;
  }
  return acc;
}, {});

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

const items = [{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
},
{
&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;
}
]
const itemsMap = items.reduce((acc, item) =&gt; {
// add it to the map if it has a nome
if (item.hasOwnProperty(&#39;nome&#39;)) {
acc[item.nome] = item;
}
return acc;
}, {})
console.log(itemsMap.fullcalendar ? .url)

<!-- end snippet -->

https://playcode.io/1482928

答案5

得分: -1

[
    {"id":1,"nome":"smartform","url":"smartform.php","label":"Dashboard","icon":"fas fa-th-large","data_attribute":"","parent":"Smartform"},
    {"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"},
    {"id":3,"nome":"fullcalendar","url":"fullcalendar.php","label":"Calendario","icon":"far fa-calendar","data_attribute":"","parent":"Tools"},
    {"id":4,"nome":"gantt","url":"gantt.php","label":"Gantt","icon":"fas fa-stream","data_attribute":"","parent":"Tools"},
    {"id":5,"nome":"timesheet","url":"timesheet.php","label":"Timesheet","icon":"fas fa-hourglass","data_attribute":"","parent":"Tools"},
    {"id":6,"nome":"kanban","url":"kanban.php","label":"Kanban","icon":"fas fa-list-ul","data_attribute":"","parent":"Tools"},
    {"id":7,"nome":"openpoints","url":"items.php?tipo=openpoints","label":"Open Points","icon":"fas fa-keyboard","data_attribute":"","parent":"Risk Management"},
    {"id":8,"nome":"risks","url":"items.php?tipo=risks","label":"Rischi","icon":"fas fa-exclamation","data_attribute":"","parent":"Risk Management"},
    {"id":9,"nome":"issues","url":"items.php?tipo=issues","label":"Issue","icon":"fas fa-fire","data_attribute":"","parent":"Risk Management"},
    {"id":10,"nome":"changerequests","url":"items.php?tipo=changerequests","label":"Change Requests","icon":"fas fa-plus","data_attribute":"","parent":"Risk Management"}
].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
    您可以通过.来访问属性。

英文:
[
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;},
    {&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;}
].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:

确定