英文:
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 = [
{"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)
<!-- 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 = "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.");
}
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 = [
{"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;
}
// more flexible option
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'));
<!-- 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 = [{
"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 itemsMap = items.reduce((acc, item) => {
// add it to the map if it has a nome
if (item.hasOwnProperty('nome')) {
acc[item.nome] = item;
}
return acc;
}, {})
console.log(itemsMap.fullcalendar ? .url)
<!-- end snippet -->
答案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
-
filter
可以使用filter()函数来获取符合您提供的选项的元素。您可以在此文档中了解更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter -
[0]
如果您想要从中获取一个项目,您可以通过索引切片来获取第一个项目。但是您需要处理过滤后的数组长度为0的情况,然后[0]可能会是未定义的。 -
.url
您可以通过.
来访问属性。
英文:
[
{"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
-
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 -
[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. -
.url
you can access to property by.
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论