0

I am trying to write an expressive JSON-representation where I try to do object composition, setting key values directly at the composition. However I can't compose multiple objects in a nested fashion.

What I want to achieve is an expressive way to write object data-structures with reusable objects in a short and neat way as a replacement for JSX -- that is why I wish I could "reuse" these object components and try to set the children key with an equal sign at composition. I only want to implement native JavaScript without libraries. JSX I'm guessing, if anybody knows, is probably converting HTML tags from strings with regular expressions? I would prefer a solution using object literals. Any ideas would be appreciated.

Example:

const foo = {
    name: 'Bob',
    abilities: {
        superpower: 'Says grrr',
        mood: 'Gruchy'
    }
}

const bar = {
    name: 'Denny',
    abilities: {
        superpower: 'Diabetes',
        mood: 'Hopeful'
    }
}

const dataStructure = {
    name: 'something',
    children: [
        foo.children = [ // 'foo' object will result in an array, even if we only set .children
            bar.children = [
                {
                    name: 'somethingElse'
                }
            ]
        ]
    ]
}

/*
    Desired resulting data structure:

    {
        name: 'something',
        children: [
            {
                name: 'Bob',
                abilities: {
                    superpower: 'Says grrr',
                    mood: 'Gruchy'
                },
                children = [
                    {
                        name: 'Denny',
                        abilities: {
                            superpower: 'Diabetes',
                            mood: 'Hopeful'
                        },
                        children = [
                            {
                                name: 'somethingElse'
                            }
                        ]
                    }
                ]
            }
        ]
    }
*/
4
  • What output would you expect from that? What code have you tried already? Commented Aug 24, 2020 at 8:21
  • @str There is no script apart from the actual object composition. See the 'desired data structure' example. Commented Aug 24, 2020 at 8:26
  • @DarkCodeWiz seems you have an error in your resulting structure. It should not have = inside (because it is "resulting") Commented Aug 24, 2020 at 9:35
  • @Anton Well, how can I reuse objects in a deeply nested composition otherwise is the question. =) Commented Aug 24, 2020 at 10:01

2 Answers 2

1

Here is working example:

const foo = {
  name: 'Bob',
  abilities: {
    superpower: 'Says grrr',
    mood: 'Gruchy'
  }
}

const bar = {
  name: 'Denny',
  abilities: {
    superpower: 'Diabetes',
    mood: 'Hopeful'
  }
}

const dataStructure = {
  name: 'something',
  children: [{
    ...foo,
    children: foo.children = [{
      ...bar,
      children: bar.children = [{
        name: 'somethingElse'
      }]
    }]
  }]
}

console.log('Result:', dataStructure)
console.log('Foo:', foo)
console.log('Bar:', bar)

Note: but this is very strange what you are trying to do...

Sign up to request clarification or add additional context in comments.

1 Comment

Don't you know that the customer is always right. ;) Writing large nested DOM structures this way would be benefited by more concise reading. Any suggestion how you would represent virtual DOM instead would be appreciated! I would like to work with object literals though.
0

you have to set your bar.children outside of your variable definition, so it wont be called another time i guess. Having this expression:

bar.children = 
{
    name: 'somethingElse'
}

const dataStructure = {
    name: 'something',
    children: 
        [foo,
        children = [bar]]
}

will give you the follwing output:

{
    "name": "something",
    "children": [
        {
            "name": "Bob",
            "abilities": {
                "superpower": "Says grrr",
                "mood": "Gruchy"
            }
        },
        [
            {
                "name": "Denny",
                "abilities": {
                    "superpower": "Diabetes",
                    "mood": "Hopeful"
                },
                "children": {
                    "name": "somethingElse"
                }
            }
        ]
    ]
}

2 Comments

It seems declaring keys directly on composed objects isn't supported natively but there must be alternatives to achieve this. I want to write expressive JSON for deeply nested data-structures instead of writing JSX. Your example produces incorrect data structure since children should always only contain objects.
Nice try though! The important part is that the solution gives good readability for large data structured, in this case representing virtual DOM.

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.