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;








0















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!










share|improve this question



















  • 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

















0















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!










share|improve this question



















  • 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













0












0








0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












3 Answers
3






active

oldest

votes


















3














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));








share|improve this answer

























  • 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











  • 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











  • Wait, that is not a string. You mean var properties = "Obj.children[0].children[0].children[2]"?

    – trincot
    Mar 23 at 19:17


















2














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))








share|improve this answer























  • 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


















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)






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    3














    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));








    share|improve this answer

























    • 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











    • 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











    • Wait, that is not a string. You mean var properties = "Obj.children[0].children[0].children[2]"?

      – trincot
      Mar 23 at 19:17















    3














    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));








    share|improve this answer

























    • 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











    • 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











    • Wait, that is not a string. You mean var properties = "Obj.children[0].children[0].children[2]"?

      – trincot
      Mar 23 at 19:17













    3












    3








    3







    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));








    share|improve this answer















    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));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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











    • 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

















    • 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











    • 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











    • Wait, that is not a string. You mean var 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













    2














    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))








    share|improve this answer























    • 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















    2














    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))








    share|improve this answer























    • 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













    2












    2








    2







    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))








    share|improve this answer













    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))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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











    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)






    share|improve this answer



























      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)






      share|improve this answer

























        1












        1








        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)






        share|improve this answer













        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)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 16:46









        richard nelsonrichard nelson

        11816




        11816



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript