簡體   English   中英

如何將 object “展平”為 arrays 數組

[英]How do I “flatten” an object into an array of arrays

我有這個 object:

{
  "value": "face",
  "next": [
  {
    "value": "tace",
    "next": [
      {
        "value": "tale",
        "next": [
          {
            "value": "talk",
            "next": []
          }
        ]
      },
      {
        "value": "tack",
        "next": [
          {
            "value": "talk",
            "next": []
          }
        ]
      }
    ]
  },
  {
    "value": "fack",
    "next": [
      {
        "value": "tack",
        "next": [
          {
            "value": "talk",
            "next": []
          }
        ]
      },
      {
        "value": "falk",
        "next": [
          {
            "value": "talk",
            "next": []
          }
        ]
      }
    ]
  }
]
}

迭代它並創建這個 arrays 數組的最佳方法是什么:

[
    ["face", "tace", "tale", "talk"],
    ["face", "tace", "tack", "talk"],
    ["face", "fack", "tack", "talk"],
    ["face" ,"fack", "falk", "talk"]
]

我基本上想通過遍歷 object 的每個分支並為每個分支生成一個字符串數組,將 object “扁平化”為數組格式。

您可以通過使用reduce方法創建遞歸 function 來執行此操作,該方法將存儲先前的值,並且next屬性中沒有元素時,它將當前復制推送到結果數組。

 const data = {"value":"face","next":[{"value":"tace","next":[{"value":"tale","next":[{"value":"talk","next":[]}]},{"value":"tack","next":[{"value":"talk","next":[]}]}]},{"value":"fack","next":[{"value":"tack","next":[{"value":"talk","next":[]}]},{"value":"falk","next":[{"value":"talk","next":[]}]}]}]} const flatten = (obj, prev = []) => { const next = prev.concat(obj.value) return obj.next.reduce((r, e) => { if(e.next.length) r.push(...flatten(e, next)) else r.push(next.slice().concat(e.value)); return r; }, []) } const result = flatten(data); console.log(result);

您可以使用獨立的遞歸 function 並收集最后一項並為每個級別構建一個給定值的數組。

 const flat = (value, next) => next.reduce((r, { value, next }) => { if (next.length) r.push(...flat(value, next)); else r.push([value]); return r; }, []).map(q => [value, ...q]); var data = { value: "face", next: [{ value: "tace", next: [{ value: "tale", next: [{ value: "talk", next: [] }] }, { value: "tack", next: [{ value: "talk", next: [] }] }] }, { value: "fack", next: [{ value: "tack", next: [{ value: "talk", next: [] }] }, { value: "falk", next: [{ value: "talk", next: [] }] }] }] }, result = flat(data.value, data.next); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

您可以使用帶有Array.forEach()的遞歸來迭代next屬性,並添加前面的項目。 next為空時,取出所有內容,展平,然后推送到結果:

 const flatAll = (data) => { const result = []; const fn = ({ value, next }, prev = []) => { if(next.length) next.forEach(o => fn(o, [prev, value])); else result.push([prev, value].flat(Infinity)); }; fn(data); return result; } const data = {"value":"face","next":[{"value":"tace","next":[{"value":"tale","next":[{"value":"talk","next":[]}]},{"value":"tack","next":[{"value":"talk","next":[]}]}]},{"value":"fack","next":[{"value":"tack","next":[{"value":"talk","next":[]}]},{"value":"falk","next":[{"value":"talk","next":[]}]}]}]}; const result = flatAll(data); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM