簡體   English   中英

JavaScript-如何動態創建對象的嵌套數組?

[英]JavaScript - How to dynamically create a nested array of objects?

我提供了一個字符串B1-FEOL-SPUTTER-0015 ,該字符串將由- (破折號)分割,然后用於構建嵌套的對象數組。 這是原始樹:

[{
  "text": "B1",
  "nodes": [{
    "text": "FEOL",
    "nodes": [{
      "text": "SPUTTER"
    }, {
      "text": "COATING"
    }, {
      "text": "EXPOSING"
    }, {
      "text": "DEVELOP"
    }, {
      "text": "PLATING"
    }, {
      "text": "BOND"
    }]
  }, {
    "text": "BEOL",
    "nodes": [{
      "text": "GRINDING"
    }, {
      "text": "BALLDROP"
    }, {
      "text": "PROBING"
    }, {
      "text": "BACKCOATING"
    }, {
      "text": "MARKING"
    }, {
      "text": "SORTING"
    }, {
      "text": "TUG"
    }]
  }]
}, {
  "text": "B2",
  "nodes": [{
    "text": "FEOL",
    "nodes": [{
      "text": "SPUTTER"
    }, {
      "text": "COATING"
    }, {
      "text": "EXPOSING"
    }, {
      "text": "DEVELOP"
    }, {
      "text": "PLATING"
    }, {
      "text": "BOND"
    }]
  }, {
    "text": "BEOL",
    "nodes": [{
      "text": "GRINDING"
    }, {
      "text": "BALLDROP"
    }, {
      "text": "PROBING"
    }, {
      "text": "BACKCOATING"
    }, {
      "text": "MARKING"
    }, {
      "text": "SORTING"
    }, {
      "text": "TUG"
    }]
  }]
}]

我嘗試通過將新值輸入.forEach(manipulateTree)來使用遞歸函數。 但是我不能在else語句中完成在數組中創建新節點的工作。

import fs from 'fs'
import path from 'path'
import util from 'util'

// This is avaliable in the link below, leading to pastebin
let tree = require('./server/configs/tree.json')

// The values I used to test:

// This works
// const newBranch = 'B1-FEOL-SPUTTER-0015'

// This doesn't
const newBranch = 'B3-ASDF-DSDF987SDF7-0015'

const locations = newBranch.split('-')

let nodes = {}

let iteration = -1
const manipulateTree = branch => {
  iteration++
  // Found node
  if (branch.text === locations[iteration]) {
    // Check if the iteration is second last in the branch array
    if (iteration === locations.length - 2) {
      branch.nodes.push({
        'text': locations[iteration + 1],
        'location': locations.join('-')
      })
    } else {
      // If not then the recursive function will continue operation
      if (branch.nodes && branch.nodes.length > 0) {
        const found = branch.nodes.find(node => node.text === locations[iteration + 1])
        if (found) {
          branch.nodes.forEach(manipulateTree)
        }
      }
    }
  } else {
    console.log('No such node')
    if (iteration === locations.length - 1) {
      branch = {
        'text': locations[iteration],
        'location': locations.join('-')
      }
    } else {
      branch = {
        'text': locations[iteration],
        'nodes': []
      }
    }
  }
  return branch
}

if (tree && tree.length > 0) {
  tree.forEach(manipulateTree)
}

console.log(util.inspect(tree, false, null, true))

我想要的最終結果是:

[{
  "text": "B1",
  "nodes": [{
    "text": "FEOL",
    "nodes": [{
      "text": "SPUTTER"
    }, {
      "text": "COATING"
    }, {
      "text": "EXPOSING"
    }, {
      "text": "DEVELOP"
    }, {
      "text": "PLATING"
    }, {
      "text": "BOND"
    }]
  }, {
    "text": "BEOL",
    "nodes": [{
      "text": "GRINDING"
    }, {
      "text": "BALLDROP"
    }, {
      "text": "PROBING"
    }, {
      "text": "BACKCOATING"
    }, {
      "text": "MARKING"
    }, {
      "text": "SORTING"
    }, {
      "text": "TUG"
    }]
  }]
}, {
  "text": "B2",
  "nodes": [{
    "text": "FEOL",
    "nodes": [{
      "text": "SPUTTER"
    }, {
      "text": "COATING"
    }, {
      "text": "EXPOSING"
    }, {
      "text": "DEVELOP"
    }, {
      "text": "PLATING"
    }, {
      "text": "BOND"
    }]
  }, {
    "text": "BEOL",
    "nodes": [{
      "text": "GRINDING"
    }, {
      "text": "BALLDROP"
    }, {
      "text": "PROBING"
    }, {
      "text": "BACKCOATING"
    }, {
      "text": "MARKING"
    }, {
      "text": "SORTING"
    }, {
      "text": "TUG"
    }]
  }]
}, { // This hear is the end result I want if there is no such node from the original tree
  "text": "B3",
  "nodes": [{
    "text": "ASDF",
    "nodes": [{
      "text": "DSDF987SDF7",
      "nodes": [{
        "text": "0015",
        "location": "B3-ASDF-DSDF987SDF7-0015"
      }]
    }]
  }]
}]

給定樹結構和路徑,您只需要迭代路徑並跟蹤您的位置。 如果您遇到的地方不存在,請添加並繼續。 您可以跟蹤索引,以便知道何時擊中最后一項,並應添加location而不是nodes數組:

 let tree = [{"text": "B1","nodes": [{"text": "FEOL","nodes": [{"text": "SPUTTER"}, {"text": "COATING"}, {"text": "EXPOSING"}, {"text": "DEVELOP"}, {"text": "PLATING"}, {"text": "BOND"}]}, {"text": "BEOL","nodes": [{"text": "GRINDING"}, {"text": "BALLDROP"}, {"text": "PROBING"}, {"text": "BACKCOATING"}, {"text": "MARKING"}, {"text": "SORTING"}, {"text": "TUG"}]}]}, {"text": "B2","nodes": [{"text": "FEOL","nodes": [{"text": "SPUTTER"}, {"text": "COATING"}, {"text": "EXPOSING"}, {"text": "DEVELOP"}, {"text": "PLATING"}, {"text": "BOND"}]}, {"text": "BEOL","nodes": [{"text": "GRINDING"}, {"text": "BALLDROP"}, {"text": "PROBING"}, {"text": "BACKCOATING"}, {"text": "MARKING"}, {"text": "SORTING"}, {"text": "TUG"}]}]}] const newBranch = 'B3-ASDF-DSDF987SDF7-0015' let components = newBranch.split('-') let nodes = tree // nodes is the current array components.forEach((text, i) => { let current = nodes.find(i => i.text == text ) // find matching item if (!current) { current = i == components.length - 1 // if no matching item add it ? {text, location: newBranch } : {text, nodes:[] } nodes.push(current) } else if (!current.nodes) current.nodes = [] nodes = current.nodes }) console.log(tree) 

暫無
暫無

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

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