【问题标题】:Update nested array with conditions in javascript用javascript中的条件更新嵌套数组
【发布时间】:2021-10-27 05:39:17
【问题描述】:

我想在 JavaScript 中创建嵌套数组。 我将得到这样的对象作为输入: [{size: 12, "text": "aa"}, {size: 11, "text": "ab"}, {size: 10, "text": "ac"}, {size: 12, "text": "ad"}...]

我想比较大小并创建类似于下面给出的结构的数组。我想使用引用来创建相同的,但 JavaScript 没有引用。 o/p 数组会是这样的:

[{
        "size": 12,
        "text": "a",
        "childrens": [{
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            },
            {
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            }
        ]
    },
    {
        "size": 12,
        "text": "a",
        "childrens": [{
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            },
            {
                "size": 11,
                "text": "a",
                "childrens": [{
                        "size": 10,
                        "text": "a",
                        "childrens": [{
                            "size": 9,
                            "text": "a",
                            "childrens": []
                        }]
                    },
                    {
                        "size": 10,
                        "text": "a",
                        "childrens": []
                    }
                ]
            }
        ]
    }
]

我试过了:

let start = [];
inp.forEach(key => {
   let found = false;
   while(!found) {
      if(start.length === 0 || start[start.length - 1].size === key.size || key.size > start[start.length - 1].size ) {
         start.push({...key, childrens: []})
    found = true;
      }
      else if(start[start.length - 1].size > key.size) {
         start = start[start.length - 1].childrens;
      }
  }
})

但在这我知道我正在重新分配开始,这似乎是错误的。 那么如何创建上面给出的数组结构呢? 提前致谢。

【问题讨论】:

  • 您能否提供更具体且相互关联的输入和输出?我不明白我们需要对孩子中的文本做什么以及为什么孩子也是数组。
  • 您将遍历输入数组,如果该元素的大小大于或等于最后一个数组索引元素的大小,那么您将在数组中创建新元素,如果元素的大小为小于当前数组索引的大小,那么您将转到最后一个数组索引的子级。您将重复此操作,直到找到插入该元素的位置。
  • 例如。在上面给定的 i/p 数组中:第一个元素的大小为 12,并且数组没有任何元素,因此您将默认插入它。第二个元素的大小为 11,小于 12,然后您将其作为 12 的子元素插入。第三个元素是
  • 你可以假设这是索引。比如1、1.1、1.1.1、1.2、2、2.1、2.1.1、2.1.2

标签: javascript node.js arrays


【解决方案1】:
let inputArr = [{size: 12, "text": "aa"}, {size: 11, "text": "ab"}, {size: 10, "text": "ac"}, {size: 12, "text": "ad"}]
let outputArr = [];

let findPlace = (output, elem) => {
  if (output.length === 0) {
    output.push(elem)
  }
  else {
    let latestOuputElem = output[output.length - 1]
    if (elem.size >= latestOuputElem.size) {
      output.push(elem)
    } else {
      if (!latestOuputElem.childrens) {
        latestOuputElem.childrens = []
      }
      findPlace(latestOuputElem.childrens, elem)
    }
  }
}

inputArr.forEach(elem => findPlace(outputArr, elem))

// result
// outputArr = [{"size":12,"text":"aa","childrens":[{"size":11,"text":"ab","childrens":[{"size":10,"text":"ac"}]}]},{"size":12,"text":"ad"}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2020-10-15
    • 1970-01-01
    • 2020-07-26
    • 2013-12-26
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多