JavaScript的嵌套对象多维数组递归函数 [英] Javascript Nested object to multidimensional array recursive function

查看:261
本文介绍了JavaScript的嵌套对象多维数组递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧这里有一个在此我一直在抓我的头在没有到目前为止巨大的成功;对不起提前很长的问题...

Ok here's one at which I've been scratching my head at without great success so far; Sorry in advance for the very long question...

我用这 Lucene的查询分析器解析字符串/查询产生本一种数据结构:

I am using this Lucene Query Parser to parse a string/query which produce this kind of data structure:

// Notice that the repetition of 'field3' is on purpose
Sample String: field1:val1 AND field2:val2 OR field3:val3 AND field3:val4
Result:
    { left: { field: "field1", term: "val1" },
      operator: "AND"
      right: {
          left: { field: "field2", term: "val2" },
          operator: "OR"
          right: { 
              left: {field: "field3", term: "val3" },
              operator: "AND",
              right: {
                   field: "field3",
                   term: "val4"
              }
          }
      }


我需要遍历该对象得到如下:


I need to iterate on that object to obtain the following:

[ [{ field: "field1", term: "val1"},
   { field: "field2", term: "val2"}
  ],
  [{ field: "field3", term: "val3"},
   { field: "field3", term: "val4"}
  ]
]

如果我试图解释这一点,这个想法是要创造一个每个子阵列由OR隔开数组的数组,而孩子阵列内部的每个对象重新presents了,隔开领域;虽然我觉得上面的code解释它比我好

If I try to explain this, the idea is to create an array of arrays where each child array are separated by "OR", while each objects inside the child arrays represents the "AND" separated fields; Although I think the code above explains it better than me

更新code CoffeeScript的并的 LO-破折号,不好意思):

Updated code (coffeescript and lo-dash, sorry):

groups = []     
createGroups = (item, previousGroup, previousOperator) ->
    currentGroup = if _.isArray previousGroup then previousGroup else []

    # keyVal = {}
    # keyVal[item.left?.field or item.field] =  item.left?.term or item.term
    obj = fieldName: item.left?.field or item.field, val: item.left?.term or item.term

    if previousOperator?.toUpperCase() is 'AND'
        currentGroup.push obj
    else
        currentGroup = [obj]

    if _.isObject item.right
        createGroups(item.right, currentGroup, item.operator)

    groups.push currentGroup

这code ++工程,和pretty很多做我想做的,而是靠在函数外声明数组(精细)但作为是直接的功能,这是不太理想的里面,但我可以忍受。

This code works, and pretty much do what I want, but rely on the groups array to be declared outside the function (fine), but used as is directly inside the function, which is not quite ideal, but I can live with it.

但是,它会复制每一个群体像这样:

However, it will duplicate every groups like so:

[ [ {field: "field1", val:val1}, {field: "field2" val:val2} ], [ {field: "field1":val1}, {field: "field2", val:val2} ], ...]

现在我必须使用 _。uniq的(组)这是一个操作我不应该有做的,如果上面的函数将返回正确的结果。

For now I have to use _.uniq(groups) which is an operation I shouldnt have to do, if the above function would return the correct results

感谢您的帮助。

推荐答案

我觉得这应该做到这一点:

I think this ought to do it:

createGroups = (item, previousGroup) ->
  subroutine = (item, previousGroup) ->
    if typeof item is "object"
      unless item.operator
        if !item.left
          previousGroup.push item  
        else
          previousGroup.push item.left
        previousGroup
      if item.operator is "AND"
        currentGroup = subroutine(item.left, previousGroup)
        currentGroup = subroutine(item.right, currentGroup)
        currentGroup and groups.push(currentGroup)
      if item.operator is "OR"
        currentGroup = subroutine(item.left, previousGroup)
        groups.push currentGroup
        currentGroup and subroutine(item.right, [])
    return

  previousGroup = previousGroup or []
  subroutine item, previousGroup
  groups

createGroups o

这篇关于JavaScript的嵌套对象多维数组递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆