I am provided with a string B1-FEOL-SPUTTER-0015, the string will be split up by - (the dash) then used to build up a nested array of objects. This is the original 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"
}]
}]
}]
I have tried using recursive functions by entering the new values into .forEach(manipulateTree). However I couldn't get it to create new nodes in the array which should be done in the else statement.
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))
The end result that I want is this:
[{
"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"
}]
}]
}]
}]
1 Answer 1
Given the tree structure and a path, you just need to iterate the path and keep track of where you are. If you end up someplace that doesn't exist yet add it and continue. You can keep track of the index so you know when you've hit the last item and should add the location rather than the nodes array:
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)
1 Comment
Explore related questions
See similar questions with these tags.
B1-FEOL-SPUTTER-015, where is all the rest of that result coming from?forEachdoesn't do anything with the return value of the callback function, what's the point ofreturn branch?