1

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.

3
  • 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? Commented Nov 24, 2018 at 16:23
  • My problem begins on the dynamic keys that will have dynamic object stored inside them. Commented Nov 24, 2018 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. Commented Nov 24, 2018 at 16:26

1 Answer 1

2

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"
        }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.