There is a variable called 'gltf.scene'.
There are many many children inside children.
gltf.scene.children[a].children[b]......children[unknown]
The children num is unknown without using console.log.
One of my target children name called 'the answer'
Can I found the children by using the array method in javascript?
The target children location is likely as below:
console.log(gltf.scene.children[0].children[0].children[0].children[0].children[6].children[0].children[3].name) //the answer
I search from stack: Find an object inside an object
However, there is unknown children layer of my case.
CodePudding user response:
If the child nesting is too deep, recursion will cause a stack overflow. An alternative is to use iteration:
function findNode (propertyName, value, initialNode) {
const stack = [initialNode];
while (true) {
const node = stack.pop();
if (!node) throw new Error('Node not found');
if (node[propertyName] === value) return node;
for (const childNode of node.children ?? []) stack.push(childNode);
}
}
const gtlf = {
scene: {
children: [{
children: [{
children: [{
children: [
{ children: [] },
{
children: [
{ children: [] },
{ children: [{}] },
{
children: [{
children: [{
children: [{}],
message: 'you found me',
name: 'the answer',
}],
}],
},
],
},
],
}],
}],
}],
},
};
const result = findNode('name', 'the answer', gtlf.scene);
console.log(result);
CodePudding user response:
you can use a recursive function to find it
const findKeyWithName = (data, name) => {
return data.children.reduce((res, d ) => {
if(res){
return res
}
if(d.name === name){
return d
}
return findKeyWithName(d, name)
}, undefined)
}
const example1 = {children: [{name: 'the answer'}]}
const example2 = {children: [{name: 'not the answer', children: [{name: 'the answer'}]}]}
const example3 = {children: [{name: 'not the answer', children: [{name: 'not the answer2', children: [{name: 'the answer'}]}]}]}
console.log(findKeyWithName(example1, 'the answer'))
console.log(findKeyWithName(example2, 'the answer'))
console.log(findKeyWithName(example3, 'the answer'))
CodePudding user response:
Use a recursive function.
I borrowed the object in the question you linked, but the solution works regardless of the number of children or the depth of the nesting.
const test = { name: "One", ids: { id: 1, level: 0 }, children: [ { name: "Two", ids: { id: 2, level: 1 }, children: [ { name: "Three", ids: { id: 3, level: 2 }, children: [ { name: "Five", ids: { id: 5, level: 3 }, children: [ { name: "Eight", ids: { id: 8, level: 4 }, children: [] }, { name: "Nine", ids: { id: 9, level: 4 }, children: [] }, { name: "Ten", ids: { id: 10, level: 4 }, children: [] } ] } ] }, { name: "Four", ids: { id: 4, level: 2 }, children: [ { name: "Six", ids: { id: 6, level: 3 }, children: [] }, { name: "Seven", ids: { id: 7, level: 3 }, children: [] } ] } ] } ] }
function findChild(obj, field, value) {
if (obj[field] === value) return obj
for (let child of obj.children) {
const result = findChild(child, field, value);
if (result) return result
}
}
console.log(findChild(test, 'name', 'Seven'))