简体   繁体   English

有条件地从 javascript 中的嵌套数组中提取值

[英]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.

相关问题 如何从嵌套在数组 JAVASCRIPT 内的对象中提取值 - how to extract values from an objects nested inside array JAVASCRIPT 从Javascript数组中提取值 - Extract values from Javascript Array Javascript - 从嵌套对象中提取值 - Javascript - Extract values from nested object 如何从嵌套在 object 内的数组中的 object 中提取值并将其存储在不同的 arrays Z53C6C951F4E558B9DFF298869 - how to extract values from an object nested inside array inside object and store it in different arrays JAVASCRIPT 从多维数组JavaScript中提取特定值 - Extract specific values from multidimensional array javascript 从javascript中的对象数组中提取匹配值 - Extract matching values from array of objects in javascript 使用 Beautiful Soup 从 JavaScript 中提取数组值 - Extract array values from JavaScript with Beautiful Soup 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 将值从字符串提取到Javascript中的数组 - Extract values from a string to an array in Javascript 如何从Javascript中的数组数组中提取值? - How to extract values from an array of arrays in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM