Creating Dynamic Keys In Object












0















I am struggling to create dynamic nested keys in javascript. My problem is that I need to have a object like this



{
"grandGrandFather": 'A',
"firstGrandFather": {
name: "AA",
children: {
first: 'AAA',
children: {
first: "AAAA"
}
}
},
"secondGrandFather": {
name: "AB",
first: 'ABA',
children: {
first: "ABAA",
second: "ABAB"
}
}
},
"thirdGrandFather": {
name: "AC",
children: {
name: "ACA"

}
}
}


Here problem is that I need to fetch these data from somewhere and I need to create these values dynamically. The prop creation begins from the first level and goes upto fourth level. So my question is how can I create dynamic keys in JS. Also I know you can create dynamic keys in JS like this:



var obj = {
prop1: "a",
prop2: "b",
prop3:'c'

}
obj['prop4'] = 'd';


Also I have been successful in creating a props to a level but when I need to stack these I get confused any help would be appreciated.



Further details about above object



In the above object I created I am getting data from database and I need to add firstGrandFather, secondGrandFather, thirdGrandFather dynamically. Also I don't know what their children props I need to define would be in the object. Some may have spouse,age,work props inside some may not also I don't know how many of these I would get. And these goes on for one or two more level.



In php it would be easy to create these in associative array easily but I am having hard time doing it in JS.










share|improve this question























  • maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

    – Nina Scholz
    Nov 24 '18 at 16:23











  • My problem begins on the dynamic keys that will have dynamic object stored inside them.

    – Rajesh Paudel
    Nov 24 '18 at 16:23






  • 1





    the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

    – Nina Scholz
    Nov 24 '18 at 16:26
















0















I am struggling to create dynamic nested keys in javascript. My problem is that I need to have a object like this



{
"grandGrandFather": 'A',
"firstGrandFather": {
name: "AA",
children: {
first: 'AAA',
children: {
first: "AAAA"
}
}
},
"secondGrandFather": {
name: "AB",
first: 'ABA',
children: {
first: "ABAA",
second: "ABAB"
}
}
},
"thirdGrandFather": {
name: "AC",
children: {
name: "ACA"

}
}
}


Here problem is that I need to fetch these data from somewhere and I need to create these values dynamically. The prop creation begins from the first level and goes upto fourth level. So my question is how can I create dynamic keys in JS. Also I know you can create dynamic keys in JS like this:



var obj = {
prop1: "a",
prop2: "b",
prop3:'c'

}
obj['prop4'] = 'd';


Also I have been successful in creating a props to a level but when I need to stack these I get confused any help would be appreciated.



Further details about above object



In the above object I created I am getting data from database and I need to add firstGrandFather, secondGrandFather, thirdGrandFather dynamically. Also I don't know what their children props I need to define would be in the object. Some may have spouse,age,work props inside some may not also I don't know how many of these I would get. And these goes on for one or two more level.



In php it would be easy to create these in associative array easily but I am having hard time doing it in JS.










share|improve this question























  • maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

    – Nina Scholz
    Nov 24 '18 at 16:23











  • My problem begins on the dynamic keys that will have dynamic object stored inside them.

    – Rajesh Paudel
    Nov 24 '18 at 16:23






  • 1





    the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

    – Nina Scholz
    Nov 24 '18 at 16:26














0












0








0








I am struggling to create dynamic nested keys in javascript. My problem is that I need to have a object like this



{
"grandGrandFather": 'A',
"firstGrandFather": {
name: "AA",
children: {
first: 'AAA',
children: {
first: "AAAA"
}
}
},
"secondGrandFather": {
name: "AB",
first: 'ABA',
children: {
first: "ABAA",
second: "ABAB"
}
}
},
"thirdGrandFather": {
name: "AC",
children: {
name: "ACA"

}
}
}


Here problem is that I need to fetch these data from somewhere and I need to create these values dynamically. The prop creation begins from the first level and goes upto fourth level. So my question is how can I create dynamic keys in JS. Also I know you can create dynamic keys in JS like this:



var obj = {
prop1: "a",
prop2: "b",
prop3:'c'

}
obj['prop4'] = 'd';


Also I have been successful in creating a props to a level but when I need to stack these I get confused any help would be appreciated.



Further details about above object



In the above object I created I am getting data from database and I need to add firstGrandFather, secondGrandFather, thirdGrandFather dynamically. Also I don't know what their children props I need to define would be in the object. Some may have spouse,age,work props inside some may not also I don't know how many of these I would get. And these goes on for one or two more level.



In php it would be easy to create these in associative array easily but I am having hard time doing it in JS.










share|improve this question














I am struggling to create dynamic nested keys in javascript. My problem is that I need to have a object like this



{
"grandGrandFather": 'A',
"firstGrandFather": {
name: "AA",
children: {
first: 'AAA',
children: {
first: "AAAA"
}
}
},
"secondGrandFather": {
name: "AB",
first: 'ABA',
children: {
first: "ABAA",
second: "ABAB"
}
}
},
"thirdGrandFather": {
name: "AC",
children: {
name: "ACA"

}
}
}


Here problem is that I need to fetch these data from somewhere and I need to create these values dynamically. The prop creation begins from the first level and goes upto fourth level. So my question is how can I create dynamic keys in JS. Also I know you can create dynamic keys in JS like this:



var obj = {
prop1: "a",
prop2: "b",
prop3:'c'

}
obj['prop4'] = 'd';


Also I have been successful in creating a props to a level but when I need to stack these I get confused any help would be appreciated.



Further details about above object



In the above object I created I am getting data from database and I need to add firstGrandFather, secondGrandFather, thirdGrandFather dynamically. Also I don't know what their children props I need to define would be in the object. Some may have spouse,age,work props inside some may not also I don't know how many of these I would get. And these goes on for one or two more level.



In php it would be easy to create these in associative array easily but I am having hard time doing it in JS.







javascript arrays object






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 16:20









Rajesh PaudelRajesh Paudel

143111




143111













  • maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

    – Nina Scholz
    Nov 24 '18 at 16:23











  • My problem begins on the dynamic keys that will have dynamic object stored inside them.

    – Rajesh Paudel
    Nov 24 '18 at 16:23






  • 1





    the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

    – Nina Scholz
    Nov 24 '18 at 16:26



















  • maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

    – Nina Scholz
    Nov 24 '18 at 16:23











  • My problem begins on the dynamic keys that will have dynamic object stored inside them.

    – Rajesh Paudel
    Nov 24 '18 at 16:23






  • 1





    the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

    – Nina Scholz
    Nov 24 '18 at 16:26

















maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

– Nina Scholz
Nov 24 '18 at 16:23





maybe you take a different data structure for same types, like grandfather and take an array for it. where do you have a problem with creating an object?

– Nina Scholz
Nov 24 '18 at 16:23













My problem begins on the dynamic keys that will have dynamic object stored inside them.

– Rajesh Paudel
Nov 24 '18 at 16:23





My problem begins on the dynamic keys that will have dynamic object stored inside them.

– Rajesh Paudel
Nov 24 '18 at 16:23




1




1





the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

– Nina Scholz
Nov 24 '18 at 16:26





the problem with dynamic keys is the later use. if you do not know the key, you have to look it up. for a family structure you could use the same keys, without using an individual key, just functional names, like person or children with nested structures. by taking random names, you need to store them and have a greater overhead.

– Nina Scholz
Nov 24 '18 at 16:26












1 Answer
1






active

oldest

votes


















0














dynamic keys are possible in ES6+ Docs here



Basically, the syntax is:



obj[variable] = value;


Variable must contain a primitive value.





As for stacking, I'm afraid you have to get used to working with deep keys access with either dot or bracket notation. You can also assign a property of your object to a variable and then access its props.
So if obj is the object from your example:



const firstGrandfathersChildren = obj.firstGrandFather.children


It will have the following assigned:



{
first: 'AAA',
children: {
first: "AAAA"
}
}





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%2f53460072%2fcreating-dynamic-keys-in-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    dynamic keys are possible in ES6+ Docs here



    Basically, the syntax is:



    obj[variable] = value;


    Variable must contain a primitive value.





    As for stacking, I'm afraid you have to get used to working with deep keys access with either dot or bracket notation. You can also assign a property of your object to a variable and then access its props.
    So if obj is the object from your example:



    const firstGrandfathersChildren = obj.firstGrandFather.children


    It will have the following assigned:



    {
    first: 'AAA',
    children: {
    first: "AAAA"
    }
    }





    share|improve this answer




























      0














      dynamic keys are possible in ES6+ Docs here



      Basically, the syntax is:



      obj[variable] = value;


      Variable must contain a primitive value.





      As for stacking, I'm afraid you have to get used to working with deep keys access with either dot or bracket notation. You can also assign a property of your object to a variable and then access its props.
      So if obj is the object from your example:



      const firstGrandfathersChildren = obj.firstGrandFather.children


      It will have the following assigned:



      {
      first: 'AAA',
      children: {
      first: "AAAA"
      }
      }





      share|improve this answer


























        0












        0








        0







        dynamic keys are possible in ES6+ Docs here



        Basically, the syntax is:



        obj[variable] = value;


        Variable must contain a primitive value.





        As for stacking, I'm afraid you have to get used to working with deep keys access with either dot or bracket notation. You can also assign a property of your object to a variable and then access its props.
        So if obj is the object from your example:



        const firstGrandfathersChildren = obj.firstGrandFather.children


        It will have the following assigned:



        {
        first: 'AAA',
        children: {
        first: "AAAA"
        }
        }





        share|improve this answer













        dynamic keys are possible in ES6+ Docs here



        Basically, the syntax is:



        obj[variable] = value;


        Variable must contain a primitive value.





        As for stacking, I'm afraid you have to get used to working with deep keys access with either dot or bracket notation. You can also assign a property of your object to a variable and then access its props.
        So if obj is the object from your example:



        const firstGrandfathersChildren = obj.firstGrandFather.children


        It will have the following assigned:



        {
        first: 'AAA',
        children: {
        first: "AAAA"
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 16:37









        Al CherAl Cher

        1046




        1046
































            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%2f53460072%2fcreating-dynamic-keys-in-object%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin