Finding the position of super nested array of objectsDetecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?Find object by id in an array of JavaScript objectsFor-each over an array in JavaScript?
FIFO data structure in pure C
Why use a retrograde orbit?
How was the blinking terminal cursor invented?
How to generate a triangular grid from a list of points
Why do galaxies collide?
Usage of the relative pronoun "dont"
How does the Heat Metal spell interact with a follow-up Frostbite spell?
How to continually and organically let my readers know what time it is in my story?
How can we delete item permanently without storing in Recycle Bin?
Could a space colony 1g from the sun work?
What are the effects of eating many berries from the Goodberry spell per day?
Five Powers of Fives Produce Unique Pandigital Number...Solve for X..Tell me Y
How to know the path of a particular software?
301 Redirects what does ([a-z]+)-(.*) and ([0-9]+)-(.*) mean
SHAKE-128/256 or SHA3-256/512
Who is frowning in the sentence "Daisy looked at Tom frowning"?
Promotion comes with unexpected 24/7/365 on-call
Why is vowel phonology represented in a trapezoid instead of a square?
Working hours and productivity expectations for game artists and programmers
Why does the U.S military use mercenaries?
Can I pay my credit card?
Why does Taylor’s series “work”?
Cuban Primes
Is it possible to pass a pointer to an operator as an argument like a pointer to a function?
Finding the position of super nested array of objects
Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?Find object by id in an array of JavaScript objectsFor-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to find the exact position and access all properties of the super nested array of objects.
I'm struggling to create a function where if I give index number as input parameter it should give me it's position in the array and also access all the properties in return.
Here is the sample array of object
I'm OK with ES6 and above solution too
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
Yeah I know this json object is scary enough to spend time and solve. Any kind of help is greatly appriciated.
for example if my function name is findChildIndex(22) it should return me something like this x.children[0].children[0].children[2]
Thank you!
javascript arrays reactjs object ecmascript-6
add a comment |
I'm trying to find the exact position and access all properties of the super nested array of objects.
I'm struggling to create a function where if I give index number as input parameter it should give me it's position in the array and also access all the properties in return.
Here is the sample array of object
I'm OK with ES6 and above solution too
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
Yeah I know this json object is scary enough to spend time and solve. Any kind of help is greatly appriciated.
for example if my function name is findChildIndex(22) it should return me something like this x.children[0].children[0].children[2]
Thank you!
javascript arrays reactjs object ecmascript-6
1
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47
add a comment |
I'm trying to find the exact position and access all properties of the super nested array of objects.
I'm struggling to create a function where if I give index number as input parameter it should give me it's position in the array and also access all the properties in return.
Here is the sample array of object
I'm OK with ES6 and above solution too
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
Yeah I know this json object is scary enough to spend time and solve. Any kind of help is greatly appriciated.
for example if my function name is findChildIndex(22) it should return me something like this x.children[0].children[0].children[2]
Thank you!
javascript arrays reactjs object ecmascript-6
I'm trying to find the exact position and access all properties of the super nested array of objects.
I'm struggling to create a function where if I give index number as input parameter it should give me it's position in the array and also access all the properties in return.
Here is the sample array of object
I'm OK with ES6 and above solution too
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
Yeah I know this json object is scary enough to spend time and solve. Any kind of help is greatly appriciated.
for example if my function name is findChildIndex(22) it should return me something like this x.children[0].children[0].children[2]
Thank you!
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
javascript arrays reactjs object ecmascript-6
javascript arrays reactjs object ecmascript-6
edited Mar 23 at 16:45
Harsh Makadia
asked Mar 23 at 16:37
Harsh MakadiaHarsh Makadia
1,31431228
1,31431228
1
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47
add a comment |
1
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47
1
1
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47
add a comment |
3 Answers
3
active
oldest
votes
You could recursively collect the indexes in the children
arrays that lead to the target index:
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I formObj.children[0].children[0].children[2]
from the array return [0,0,2] ?
– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable asvar properties = Obj.children[0].children[0].children[2]
. If you can please help me
– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You meanvar properties = "Obj.children[0].children[0].children[2]"
?
– trincot
Mar 23 at 19:17
|
show 7 more comments
You could use recursion and check if children
of the element exists use for
loop to iterate to through all the children
and recursively apply the function of each child
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
add a comment |
If i got your question correctly, You can do something like this:
const func=(obj,index, nested=0)=>
return Obj.index===index ? obj, nested : func(obj.children,index, nested+1)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315973%2ffinding-the-position-of-super-nested-array-of-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could recursively collect the indexes in the children
arrays that lead to the target index:
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I formObj.children[0].children[0].children[2]
from the array return [0,0,2] ?
– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable asvar properties = Obj.children[0].children[0].children[2]
. If you can please help me
– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You meanvar properties = "Obj.children[0].children[0].children[2]"
?
– trincot
Mar 23 at 19:17
|
show 7 more comments
You could recursively collect the indexes in the children
arrays that lead to the target index:
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I formObj.children[0].children[0].children[2]
from the array return [0,0,2] ?
– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable asvar properties = Obj.children[0].children[0].children[2]
. If you can please help me
– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You meanvar properties = "Obj.children[0].children[0].children[2]"
?
– trincot
Mar 23 at 19:17
|
show 7 more comments
You could recursively collect the indexes in the children
arrays that lead to the target index:
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
You could recursively collect the indexes in the children
arrays that lead to the target index:
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
function findIndexNested(data, index) []).findIndex(child =>
return result = findIndexNested(child, index)
);
if (result) return [i, ...result];
function findByPath(data, path)
for (let i of path) data = data.children[i];
return data
// Sample data
const data = "name": "branch 1","index": 1,"children": ["name": "sub child 1","index": 2,"children": ["name": "subx2 child 1","index": 3,"children": ["name": "subx3 child 1","index": 4,"children": ["name": "subx4 child 1","index": 21,"name": "subx4 child 2","index": 18],"name": "subx3 child 2","index": 6,"children": ["name": "subx4 child 1","index": 7,"name": "subx4 child 2","index": 21],"name": "subx3 child 3","index": 22]],"name": "sub child 2","index": 28]
const index = 22
const result = findIndexNested(data, index);
console.log("Found index " + index + " via these child indexes: " + result);
console.log("The object is", findByPath(data, result));
edited Mar 23 at 19:37
answered Mar 23 at 16:54
trincottrincot
134k1697134
134k1697134
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I formObj.children[0].children[0].children[2]
from the array return [0,0,2] ?
– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable asvar properties = Obj.children[0].children[0].children[2]
. If you can please help me
– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You meanvar properties = "Obj.children[0].children[0].children[2]"
?
– trincot
Mar 23 at 19:17
|
show 7 more comments
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I formObj.children[0].children[0].children[2]
from the array return [0,0,2] ?
– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable asvar properties = Obj.children[0].children[0].children[2]
. If you can please help me
– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You meanvar properties = "Obj.children[0].children[0].children[2]"
?
– trincot
Mar 23 at 19:17
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
Thanks, @trincot for the help! I really appreciate it.
– Harsh Makadia
Mar 23 at 16:57
how can I form
Obj.children[0].children[0].children[2]
from the array return [0,0,2] ?– Harsh Makadia
Mar 23 at 18:32
how can I form
Obj.children[0].children[0].children[2]
from the array return [0,0,2] ?– Harsh Makadia
Mar 23 at 18:32
You mean, as a string?
– trincot
Mar 23 at 18:52
You mean, as a string?
– trincot
Mar 23 at 18:52
yes like I want to store it in variable as
var properties = Obj.children[0].children[0].children[2]
. If you can please help me– Harsh Makadia
Mar 23 at 19:14
yes like I want to store it in variable as
var properties = Obj.children[0].children[0].children[2]
. If you can please help me– Harsh Makadia
Mar 23 at 19:14
Wait, that is not a string. You mean
var properties = "Obj.children[0].children[0].children[2]"
?– trincot
Mar 23 at 19:17
Wait, that is not a string. You mean
var properties = "Obj.children[0].children[0].children[2]"
?– trincot
Mar 23 at 19:17
|
show 7 more comments
You could use recursion and check if children
of the element exists use for
loop to iterate to through all the children
and recursively apply the function of each child
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
add a comment |
You could use recursion and check if children
of the element exists use for
loop to iterate to through all the children
and recursively apply the function of each child
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
add a comment |
You could use recursion and check if children
of the element exists use for
loop to iterate to through all the children
and recursively apply the function of each child
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
You could use recursion and check if children
of the element exists use for
loop to iterate to through all the children
and recursively apply the function of each child
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
const obj =
"name": "branch 1",
"index": 1,
"children": [
"name": "sub child 1",
"index": 2,
"children": [
"name": "subx2 child 1",
"index": 3,
"children": [
"name": "subx3 child 1",
"index": 4,
"children": [
"name": "subx4 child 1",
"index": 21
,
"name": "subx4 child 2",
"index": 18
]
,
"name": "subx3 child 2",
"index": 6,
"children": [
"name": "subx4 child 1",
"index": 7
,
"name": "subx4 child 2",
"index": 21
]
,
"name": "subx3 child 3",
"index": 22
]
]
,
"name": "sub child 2",
"index": 28
]
function find(obj,index)
if(obj.children)
for(let i = 0;i<obj.children.length;i++)
let x = find(obj.children[i],index);
if(x) return ...x,pos:i;
return obj.index === index ? obj : false;
console.log(find(obj,21))
answered Mar 23 at 16:44
Maheer AliMaheer Ali
17.2k21633
17.2k21633
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
add a comment |
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
Thanks for the help. Just one question here How can I know the index position where it is present like for 22 it could be Obj.children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:50
add a comment |
If i got your question correctly, You can do something like this:
const func=(obj,index, nested=0)=>
return Obj.index===index ? obj, nested : func(obj.children,index, nested+1)
add a comment |
If i got your question correctly, You can do something like this:
const func=(obj,index, nested=0)=>
return Obj.index===index ? obj, nested : func(obj.children,index, nested+1)
add a comment |
If i got your question correctly, You can do something like this:
const func=(obj,index, nested=0)=>
return Obj.index===index ? obj, nested : func(obj.children,index, nested+1)
If i got your question correctly, You can do something like this:
const func=(obj,index, nested=0)=>
return Obj.index===index ? obj, nested : func(obj.children,index, nested+1)
answered Mar 23 at 16:46
richard nelsonrichard nelson
11816
11816
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315973%2ffinding-the-position-of-super-nested-array-of-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"it should give me it's position in the array and also access all the properties in return": please provide a concrete example of input and expected output.
– trincot
Mar 23 at 16:39
For example, my function name is findChildIndex(indexNumber) for example if 22 it should return me it's the position return me this children[0].children[0].children[2]
– Harsh Makadia
Mar 23 at 16:44
I think you need to ask yourself why this data is structured like this. Flat data is better for json.
– Joe Lloyd
Mar 23 at 16:44
@JoeLloyd This data is used for npmjs.com/package/react-d3-tree so can't help! :(
– Harsh Makadia
Mar 23 at 16:47