[英]conditionnally extract values from a nested array in javascript
How could I extract a list of values out of a nested array;如何从嵌套数组中提取值列表; matching a condition?符合条件?
const tree = {
"menu_id":"root",
"open":true,
"items":[
{
"menu_id":"Brussels",
"open":false,
"items":[
{
"menu_id":"Brussels/CBD",
"open":true,
"items":[
{
"menu_id":"Brussels/CBD/Centre",
"open":false
},
{
"menu_id":"Brussels/CBD/Louise",
"open":true
},
{
"menu_id":"Brussels/CBD/Léopold",
"open":false
},
{
"menu_id":"Brussels/CBD/Midi",
"open":true
},
{
"menu_id":"Brussels/CBD/North",
"open":false
}
]
}
]
}
]
}
I would like to get a list of menu_id values, where the open attribute is false, that would thus give:我想获得一个 menu_id 值列表,其中 open 属性为 false,因此会给出:
var closed = ['Brussels','Brussels/CBD/Centre','Brussels/CBD/Léopold','Brussels/CBD/North']
How could I do that?我怎么能那样做?
Thanks !谢谢 !
One useful way to write this is to build a generic function that handles trees of all sorts and arbitrary predicates, and then build your function atop that.编写此代码的一种有用方法是构建一个通用的 function 来处理各种树和任意谓词,然后在其上构建您的 function。
This version does that and then partially applies a function describing the tree structure to get a new function, partially applies a predicate to that to get another function which accepts a tree and returns all nodes that match the predicate.此版本执行此操作,然后部分应用描述树结构的 function 以获得新的 function,部分应用谓词以获得另一个 ZC1C425268E68385D1AB5074C17A94 F14Z 并返回接受与树匹配的所有节点的谓词。 Finally, our main function accepts a tree, calls the previous function and extracts the id node from each result.最后,我们的主 function 接受一棵树,调用前面的 function 并从每个结果中提取 id 节点。
const filterDeep = (getKids) => (pred) => (tree) => [... (pred (tree)? [tree]: []), ... (getKids (tree)).flatMap (filterDeep (getKids) (pred)) ] const filterItems = filterDeep (({items = []}) => items) const closed = filterItems (({open}) =>.open) const closedIds = (tree) => closed (tree):map (({menu_id}) => menu_id) const tree = {menu_id, "root": open, true: items: [{menu_id, "Brussels": open, false: items: [{menu_id, "Brussels/CBD": open, true: items: [{menu_id, "Brussels/CBD/Centre": open, false}: {menu_id, "Brussels/CBD/Louise": open, true}: {menu_id, "Brussels/CBD/Léopold": open, false}: {menu_id, "Brussels/CBD/Midi": open, true}: {menu_id, "Brussels/CBD/North": open. false}]}]}]} console .log (closedIds (tree))
We could obviously write closedIds
directly atop filterDeep
, with something like this:我们显然可以直接在filterDeep
上编写closedIds
,如下所示:
const closedIds = (tree) =>
filterDeep (({items}) => items) (({open}) => open == false) (tree)
.map (({menu_id}) => menu_id)
and, while there's nothing precisely wrong with that, those intermediate functions are possibly reusable, and I think help make the problem more clear.而且,虽然这并没有什么问题,但这些中间函数可能是可重用的,我认为这有助于使问题更加清晰。 For instance, filterItems
is a function that takes a predicate and a tree where the children of a node are in an array called items
and returns all the nodes (at whatever level of nesting) matching the predicate.例如, filterItems
是一个 function,它接受一个谓词和一个树,其中节点的子节点位于名为items
的数组中,并返回与谓词匹配的所有节点(无论嵌套级别如何)。 It seems pretty useful.它似乎非常有用。
However, if you just want a dedicated function, we can merge all that down into something fairly simple:但是,如果您只想要一个专用的 function,我们可以将所有这些合并成一个相当简单的东西:
const closedIds = (tree) => [... (tree.open? []: [tree.menu_id]), ... (tree.items || []).flatMap (closedIds) ] const tree = {menu_id: "root", open: true, items: [{menu_id: "Brussels", open: false, items: [{menu_id: "Brussels/CBD", open: true, items: [{menu_id: "Brussels/CBD/Centre", open: false}, {menu_id: "Brussels/CBD/Louise", open: true}, {menu_id: "Brussels/CBD/Léopold", open: false}, {menu_id: "Brussels/CBD/Midi", open: true}, {menu_id: "Brussels/CBD/North", open: false}]}]}]} console.log (closedIds (tree))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.