我有下面的数组,每个元素都有另一个名为FunctionalityChildren的数组,我需要找到包含属性 ActionFull 等于变量的唯一对象,例如'/ budget / allocation'或'/预算“
let bigArray = [
{
"FunctionalityID": 114,
"Name": "General Register",
"Action": "/general-register",
"Icon": "settings_input_composite",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 1,
"FunctionalityChildren": [
{
"FunctionalityID": 115,
"Name": "Supplier",
"Action": "/supplier",
"Icon": "perm_contact_calendar",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1251,
"Profile_ID": 68,
"Functionality_ID": 115,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/supplier"
},
{
"FunctionalityID": 116,
"Name": "RPA",
"Action": "/rpa",
"Icon": "view_day",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 2,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1252,
"Profile_ID": 68,
"Functionality_ID": 116,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/rpa"
},
{
"FunctionalityID": 117,
"Name": "Cost Center",
"Action": "/cost-center",
"Icon": "home",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 3,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1253,
"Profile_ID": 68,
"Functionality_ID": 117,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/cost-center"
},
{
"FunctionalityID": 118,
"Name": "Departament",
"Action": "/departament",
"Icon": "donut_small",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 4,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1254,
"Profile_ID": 68,
"Functionality_ID": 118,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/departament"
},
{
"FunctionalityID": 119,
"Name": "Product Line",
"Action": "/product-line",
"Icon": "view_headline",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 5,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1255,
"Profile_ID": 68,
"Functionality_ID": 119,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/product-line"
},
{
"FunctionalityID": 120,
"Name": "Product",
"Action": "/product",
"Icon": "shopping_cart",
"System_ID": 21,
"FunctionalityFather_ID": 114,
"Active": 1,
"Priority": 6,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1256,
"Profile_ID": 68,
"Functionality_ID": 120,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/general-register/product"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1250,
"Profile_ID": 68,
"Functionality_ID": 114,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/general-register",
"HasFunctionalities": true,
"model": false
},
{
"FunctionalityID": 99,
"Name": "Budget Account",
"Action": "/budget-account",
"Icon": "monetization_on",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 2,
"FunctionalityChildren": [
{
"FunctionalityID": 100,
"Name": "Sector",
"Action": "/sector",
"Icon": "account_balance",
"System_ID": 21,
"FunctionalityFather_ID": 99,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1258,
"Profile_ID": 68,
"Functionality_ID": 100,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget-account/sector"
},
{
"FunctionalityID": 101,
"Name": "Group",
"Action": "/group",
"Icon": "group_work",
"System_ID": 21,
"FunctionalityFather_ID": 99,
"Active": 1,
"Priority": 2,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1259,
"Profile_ID": 68,
"Functionality_ID": 101,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget-account/group"
},
{
"FunctionalityID": 102,
"Name": "Account",
"Action": "/account",
"Icon": "attach_money",
"System_ID": 21,
"FunctionalityFather_ID": 99,
"Active": 1,
"Priority": 3,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1260,
"Profile_ID": 68,
"Functionality_ID": 102,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget-account/account"
},
{
"FunctionalityID": 103,
"Name": "Budget",
"Action": "/budget",
"Icon": "credit_card",
"System_ID": 21,
"FunctionalityFather_ID": 99,
"Active": 1,
"Priority": 4,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1261,
"Profile_ID": 68,
"Functionality_ID": 103,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget-account/budget"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1257,
"Profile_ID": 68,
"Functionality_ID": 99,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/budget-account",
"HasFunctionalities": true,
"model": false
},
{
"FunctionalityID": 105,
"Name": "Budget",
"Action": "/budget",
"Icon": "credit_card",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 3,
"FunctionalityChildren": [
{
"FunctionalityID": 106,
"Name": "Allocation",
"Action": "/allocation",
"Icon": "note_add",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1272,
"Profile_ID": 68,
"Functionality_ID": 106,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/allocation"
},
{
"FunctionalityID": 107,
"Name": "Copy",
"Action": "/copy",
"Icon": "content_copy",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 2,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1273,
"Profile_ID": 68,
"Functionality_ID": 107,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/copy"
},
{
"FunctionalityID": 108,
"Name": "In And Out",
"Action": "/in-and-out",
"Icon": "swap_vertical_circle",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 3,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1274,
"Profile_ID": 68,
"Functionality_ID": 108,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/in-and-out"
},
{
"FunctionalityID": 109,
"Name": "Account Accounting",
"Action": "/account-accounting",
"Icon": "assignment",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 4,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1275,
"Profile_ID": 68,
"Functionality_ID": 109,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/account-accounting"
},
{
"FunctionalityID": 110,
"Name": "Event",
"Action": "/eventos",
"Icon": "shopping_cart",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 5,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1276,
"Profile_ID": 68,
"Functionality_ID": 110,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/eventos"
},
{
"FunctionalityID": 111,
"Name": "Copy Counter",
"Action": "/copy-counter",
"Icon": "swap_vertical_circle",
"System_ID": 21,
"FunctionalityFather_ID": 105,
"Active": 1,
"Priority": 6,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1277,
"Profile_ID": 68,
"Functionality_ID": 111,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/budget/copy-counter"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1271,
"Profile_ID": 68,
"Functionality_ID": 105,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/budget",
"HasFunctionalities": false,
"model": false
},
{
"FunctionalityID": 112,
"Name": "Config",
"Action": "/config",
"Icon": "build",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 4,
"FunctionalityChildren": [
{
"FunctionalityID": 113,
"Name": "Control Year Month",
"Action": "/control-year-month",
"Icon": "date_range",
"System_ID": 21,
"FunctionalityFather_ID": 112,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1263,
"Profile_ID": 68,
"Functionality_ID": 113,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/config/control-year-month"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1262,
"Profile_ID": 68,
"Functionality_ID": 112,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/config",
"HasFunctionalities": true,
"model": false
},
{
"FunctionalityID": 121,
"Name": "Report",
"Action": "/report",
"Icon": "picture_as_pdf",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 5,
"FunctionalityChildren": [
{
"FunctionalityID": 122,
"Name": "Report 1",
"Action": "/report-um",
"Icon": "picture_as_pdf",
"System_ID": 21,
"FunctionalityFather_ID": 121,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1265,
"Profile_ID": 68,
"Functionality_ID": 122,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/report/report-um"
},
{
"FunctionalityID": 123,
"Name": "Report 2",
"Action": "/report-dois",
"Icon": "picture_as_pdf",
"System_ID": 21,
"FunctionalityFather_ID": 121,
"Active": 1,
"Priority": 2,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1266,
"Profile_ID": 68,
"Functionality_ID": 123,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/report/report-dois"
},
{
"FunctionalityID": 124,
"Name": "Report 3",
"Action": "/report-tres",
"Icon": "picture_as_pdf",
"System_ID": 21,
"FunctionalityFather_ID": 121,
"Active": 1,
"Priority": 3,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1267,
"Profile_ID": 68,
"Functionality_ID": 124,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/report/report-tres"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1264,
"Profile_ID": 68,
"Functionality_ID": 121,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/report",
"HasFunctionalities": true,
"model": false
},
{
"FunctionalityID": 125,
"Name": "Profile",
"Action": "/profile",
"Icon": "person",
"System_ID": 21,
"FunctionalityFather_ID": null,
"Active": 1,
"Priority": 6,
"FunctionalityChildren": [
{
"FunctionalityID": 126,
"Name": "New",
"Action": "/new",
"Icon": "plus_one",
"System_ID": 21,
"FunctionalityFather_ID": 125,
"Active": 1,
"Priority": 1,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1269,
"Profile_ID": 68,
"Functionality_ID": 126,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/profile/new"
},
{
"FunctionalityID": 127,
"Name": "List",
"Action": "/list",
"Icon": "view_list",
"System_ID": 21,
"FunctionalityFather_ID": 125,
"Active": 1,
"Priority": 2,
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1270,
"Profile_ID": 68,
"Functionality_ID": 127,
"CanInsert": true,
"CanUpdate": true,
"CanDelete": true
}
],
"ActionFull": "/profile/list"
}
],
"ProfileFunctionalities": [
{
"ProfileFunctionalityID": 1268,
"Profile_ID": 68,
"Functionality_ID": 125,
"CanInsert": false,
"CanUpdate": false,
"CanDelete": false
}
],
"ActionFull": "/profile",
"HasFunctionalities": true,
"model": false
}
]
上面的代码对我有用,但我认为存在另一个代码更多的糖,我想学习它:
let all = bigArray.reduce((prev, next) => {
return prev.concat(next.FunctionalityChildren)
}, [])
bigArray.reduce((prev, next) => all.push(next), [])
let desired1 = '/budget'
let desired2 = '/budget/allocation'
let element1 = all.find(e=>e.ActionFull === desired1) // return the object that contains ActionFull = '/budget' (can be father)
let element2 = all.find(e=>e.ActionFull === desired2) // return the object that contains ActionFull = '/budget/allocation' (can be child)
console.log(element1)
console.log(element2)
如上所示,注意它可以是父亲或孩子。
答案 0 :(得分:2)
由于您必须查找唯一项目,因此您可以使用find
方法。 find()
方法返回数组中第一个满足提供的测试函数的元素的值。否则返回 undefined 。
此外,您可以使用map
方法和spread syntax来查找bigArray
元素中的所有子元素。
map()
方法创建一个new
数组,其结果是在调用数组中的每个元素上调用提供的回调函数。< / p>
let bigArray = [ { "FunctionalityID": 114, "Name": "General Register", "Action": "/general-register", "Icon": "settings_input_composite", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 1, "FunctionalityChildren": [ { "FunctionalityID": 115, "Name": "Supplier", "Action": "/supplier", "Icon": "perm_contact_calendar", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1251, "Profile_ID": 68, "Functionality_ID": 115, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/supplier" }, { "FunctionalityID": 116, "Name": "RPA", "Action": "/rpa", "Icon": "view_day", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 2, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1252, "Profile_ID": 68, "Functionality_ID": 116, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/rpa" }, { "FunctionalityID": 117, "Name": "Cost Center", "Action": "/cost-center", "Icon": "home", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 3, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1253, "Profile_ID": 68, "Functionality_ID": 117, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/cost-center" }, { "FunctionalityID": 118, "Name": "Departament", "Action": "/departament", "Icon": "donut_small", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 4, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1254, "Profile_ID": 68, "Functionality_ID": 118, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/departament" }, { "FunctionalityID": 119, "Name": "Product Line", "Action": "/product-line", "Icon": "view_headline", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 5, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1255, "Profile_ID": 68, "Functionality_ID": 119, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/product-line" }, { "FunctionalityID": 120, "Name": "Product", "Action": "/product", "Icon": "shopping_cart", "System_ID": 21, "FunctionalityFather_ID": 114, "Active": 1, "Priority": 6, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1256, "Profile_ID": 68, "Functionality_ID": 120, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/general-register/product" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1250, "Profile_ID": 68, "Functionality_ID": 114, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/general-register", "HasFunctionalities": true, "model": false }, { "FunctionalityID": 99, "Name": "Budget Account", "Action": "/budget-account", "Icon": "monetization_on", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 2, "FunctionalityChildren": [ { "FunctionalityID": 100, "Name": "Sector", "Action": "/sector", "Icon": "account_balance", "System_ID": 21, "FunctionalityFather_ID": 99, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1258, "Profile_ID": 68, "Functionality_ID": 100, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget-account/sector" }, { "FunctionalityID": 101, "Name": "Group", "Action": "/group", "Icon": "group_work", "System_ID": 21, "FunctionalityFather_ID": 99, "Active": 1, "Priority": 2, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1259, "Profile_ID": 68, "Functionality_ID": 101, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget-account/group" }, { "FunctionalityID": 102, "Name": "Account", "Action": "/account", "Icon": "attach_money", "System_ID": 21, "FunctionalityFather_ID": 99, "Active": 1, "Priority": 3, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1260, "Profile_ID": 68, "Functionality_ID": 102, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget-account/account" }, { "FunctionalityID": 103, "Name": "Budget", "Action": "/budget", "Icon": "credit_card", "System_ID": 21, "FunctionalityFather_ID": 99, "Active": 1, "Priority": 4, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1261, "Profile_ID": 68, "Functionality_ID": 103, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget-account/budget" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1257, "Profile_ID": 68, "Functionality_ID": 99, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/budget-account", "HasFunctionalities": true, "model": false }, { "FunctionalityID": 105, "Name": "Budget", "Action": "/budget", "Icon": "credit_card", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 3, "FunctionalityChildren": [ { "FunctionalityID": 106, "Name": "Allocation", "Action": "/allocation", "Icon": "note_add", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1272, "Profile_ID": 68, "Functionality_ID": 106, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/allocation" }, { "FunctionalityID": 107, "Name": "Copy", "Action": "/copy", "Icon": "content_copy", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 2, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1273, "Profile_ID": 68, "Functionality_ID": 107, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/copy" }, { "FunctionalityID": 108, "Name": "In And Out", "Action": "/in-and-out", "Icon": "swap_vertical_circle", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 3, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1274, "Profile_ID": 68, "Functionality_ID": 108, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/in-and-out" }, { "FunctionalityID": 109, "Name": "Account Accounting", "Action": "/account-accounting", "Icon": "assignment", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 4, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1275, "Profile_ID": 68, "Functionality_ID": 109, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/account-accounting" }, { "FunctionalityID": 110, "Name": "Event", "Action": "/eventos", "Icon": "shopping_cart", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 5, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1276, "Profile_ID": 68, "Functionality_ID": 110, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/eventos" }, { "FunctionalityID": 111, "Name": "Copy Counter", "Action": "/copy-counter", "Icon": "swap_vertical_circle", "System_ID": 21, "FunctionalityFather_ID": 105, "Active": 1, "Priority": 6, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1277, "Profile_ID": 68, "Functionality_ID": 111, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/budget/copy-counter" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1271, "Profile_ID": 68, "Functionality_ID": 105, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/budget", "HasFunctionalities": false, "model": false }, { "FunctionalityID": 112, "Name": "Config", "Action": "/config", "Icon": "build", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 4, "FunctionalityChildren": [ { "FunctionalityID": 113, "Name": "Control Year Month", "Action": "/control-year-month", "Icon": "date_range", "System_ID": 21, "FunctionalityFather_ID": 112, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1263, "Profile_ID": 68, "Functionality_ID": 113, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/config/control-year-month" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1262, "Profile_ID": 68, "Functionality_ID": 112, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/config", "HasFunctionalities": true, "model": false }, { "FunctionalityID": 121, "Name": "Report", "Action": "/report", "Icon": "picture_as_pdf", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 5, "FunctionalityChildren": [ { "FunctionalityID": 122, "Name": "Report 1", "Action": "/report-um", "Icon": "picture_as_pdf", "System_ID": 21, "FunctionalityFather_ID": 121, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1265, "Profile_ID": 68, "Functionality_ID": 122, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/report/report-um" }, { "FunctionalityID": 123, "Name": "Report 2", "Action": "/report-dois", "Icon": "picture_as_pdf", "System_ID": 21, "FunctionalityFather_ID": 121, "Active": 1, "Priority": 2, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1266, "Profile_ID": 68, "Functionality_ID": 123, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/report/report-dois" }, { "FunctionalityID": 124, "Name": "Report 3", "Action": "/report-tres", "Icon": "picture_as_pdf", "System_ID": 21, "FunctionalityFather_ID": 121, "Active": 1, "Priority": 3, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1267, "Profile_ID": 68, "Functionality_ID": 124, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/report/report-tres" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1264, "Profile_ID": 68, "Functionality_ID": 121, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/report", "HasFunctionalities": true, "model": false }, { "FunctionalityID": 125, "Name": "Profile", "Action": "/profile", "Icon": "person", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 6, "FunctionalityChildren": [ { "FunctionalityID": 126, "Name": "New", "Action": "/new", "Icon": "plus_one", "System_ID": 21, "FunctionalityFather_ID": 125, "Active": 1, "Priority": 1, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1269, "Profile_ID": 68, "Functionality_ID": 126, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/profile/new" }, { "FunctionalityID": 127, "Name": "List", "Action": "/list", "Icon": "view_list", "System_ID": 21, "FunctionalityFather_ID": 125, "Active": 1, "Priority": 2, "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1270, "Profile_ID": 68, "Functionality_ID": 127, "CanInsert": true, "CanUpdate": true, "CanDelete": true } ], "ActionFull": "/profile/list" } ], "ProfileFunctionalities": [ { "ProfileFunctionalityID": 1268, "Profile_ID": 68, "Functionality_ID": 125, "CanInsert": false, "CanUpdate": false, "CanDelete": false } ], "ActionFull": "/profile", "HasFunctionalities": true, "model": false } ]
let action_full = '/budget/allocation';
let result = [].concat(...bigArray.map(elem => elem.FunctionalityChildren))
.find(a => a.ActionFull == action_full); // It work only to children objects.
console.log(result);
let findedInFatherAndChild = bigArray.concat(...bigArray.map(elem => elem.FunctionalityChildren)).find(a => a.ActionFull === '/budget') // This work for me, both for father and children. Thanks Mihai
答案 1 :(得分:1)
我们希望我们的通用deepFind
功能如何运作
deepFind (x => x.ActionFull === '/budget', bigArray)
// { FunctionalityID: 105, Name: 'Budget', ActionFull: '/budget' ... }
deepFind (x => x.ActionFull === '/budget/allocation', bigArray)
// { FunctionalityID: 106, Name: 'Allocation', ActionFull: '/budget/allocation' }
我将为您提供势在必行的样式解决方案,希望这能让您思考如何构建函数调用。请注意,此程序将开始通过一组可能的解决方案进行迭代,但它会停止迭代并在找到匹配项后立即返回结果。使用Array#reduce
或Array#map
或Array#filter
不足以完成此任务,因为它们没有我们正在寻找的短路行为
在浏览器中运行以下程序,这次使用简化的数据集。应该很容易理解我们如何得出结果
const data =
[ { a: 1, b: 1 }
, { a: 2, b: 2, c: { d: [ { e: 2 } ] } }
, { a: 3, b: { c: { d: { e: { f: 3 } } } } }
]
const deepFind = (f, obj = {}) =>
{ if (Object (obj) === obj)
{ if (f (obj) === true)
return obj
for (const [ k, v ] of Object.entries (obj))
{ const res =
deepFind (f, v)
if (res !== undefined)
return res
}
}
return undefined
}
console.log
( deepFind (x => x.a === 1, data) // { a: 1, b: 1 }
, deepFind (x => x.e === 2, data) // { e: 2 }
, deepFind (x => Array.isArray(x.d), data) // { d: [ { e: 2 } ] }
, deepFind (x => x.f === 3, data) // { f: 3 }
, deepFind (x => x.e && x.e.f === 3, data) // { e: { f: 3 } }
, deepFind (x => x.z === 9, data) // undefined
)
&#13;
deepFind
适用于所有对象,包括数组
const alpha =
[ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ]
deepFind (x => x [1] === 'h', alpha)
// [ 'g', 'h', 'i', ]
deepFind (([ _0, _1, _2 ]) => _2 === 'f', alpha)
// [ 'd', 'e', 'f' ]
将deepFind
表示为功能程序只需要将迭代循环转换为具有状态参数的递归函数。因为您自己设计了递归函数,所以可以对该程序所需的短路行为进行编码。
下面,deepFind
使用纯函数表达式编写。添加了状态参数v
和rest
,但只有f
和o
由用户指定,就像我们上面所做的那样。如果您不希望将这些作为功能的公共接口的一部分,则可以使用内部辅助功能。
const identity = x =>
x
const None =
Symbol ()
const deepFind = (f = identity, o = {}, [ _, v ] = [ None, None ], ...rest) =>
Object (o) === o
? f (o) === true
? o
: v === None
? deepFind (f, null, ...Object.entries (o))
: deepFind (f, v, ...rest, ...Object.entries (o))
: v === None
? undefined
: deepFind (f, v, ...rest )
重新运行程序以验证输出确实相同
const identity = x =>
x
const None =
Symbol ()
const deepFind = (f = identity, o = {}, [ _, v ] = [ None, None ], ...rest) =>
Object (o) === o
? f (o) === true
? o
: v === None
? deepFind (f, null, ...Object.entries (o))
: deepFind (f, v, ...rest, ...Object.entries (o))
: v === None
? undefined
: deepFind (f, v, ...rest )
const data =
[ { a: 1, b: 1 }
, { a: 2, b: 2, c: { d: [ { e: 2 } ] } }
, { a: 3, b: { c: { d: { e: { f: 3 } } } } }
]
console.log
( deepFind (x => x.a === 1, data) // { a: 1, b: 1 }
, deepFind (x => x.e === 2, data) // { e: 2 }
, deepFind (x => Array.isArray(x.d), data) // { d: [ { e: 2 } ] }
, deepFind (x => x.f === 3, data) // { f: 3 }
, deepFind (x => x.e && x.e.f === 3, data) // { e: { f: 3 } }
, deepFind (x => x.z === 9, data) // undefined
)
const alpha =
[ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ]
console.log
( deepFind (x => x [1] === 'h', alpha) // [ 'g', 'h', 'i', ]
, deepFind (([ _0, _1, _2 ]) => _2 === 'f', alpha) // [ 'd', 'e', 'f' ]
)
&#13;
最后,这里是deepFindAll
。在此程序的变体中,我们得到零个或多个结果的数组,而不是单个答案或undefined
。这演示了我在前面的例子中提到的辅助循环,也是生成器的一个漂亮的用例。
作为练习,我鼓励您使用功能表达式代替辅助生成器重写deepFindAll
const deepFindAll = (f, o = {}) =>
{ const aux =
function* (f, o)
{ if (Object (o) === o)
{ if (f (o) === true)
yield o
for (const [ _, v ] of Object.entries (o))
yield* aux (f, v)
}
}
return Array.from (aux (f, o))
}
const data =
[ { a: 1, b: 1 }
, { a: 2, b: 2, c: { d: [ { e: 2 } ] } }
, { a: 3, b: { c: { d: { e: { f: 3 } } } } }
]
console.log
( deepFindAll (x => x.a === 1 || x.e === 2, data) // [ { a: 1, b: 1 }, { e: 2 } ]
, deepFindAll (x => x.e !== undefined, data) //[ { e: 2 }, { e: { f: 3 } } ]
, deepFindAll (x => x.z === 9, data) // []
)
&#13;