Steem Bounty : Nestify a flat JSON object

in javascript •  5 months ago

I recently had a requirement in a project where I had to convert a flat JSON object into a nested JSON object. Instead of explaining the problem statement I will share a set of input and output arrays so that the requirement becomes clear.

javascript.jpg

const input1 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry'
    },
    {
        pos: 2,
        text: 'David'
    },
    {
        pos: 1,
        text: 'Lisa'
    },
];
const output1 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry',
        children: [{
            pos: 2,
            text: 'David'
        }]
    },
    {
        pos: 1,
        text: 'Lisa'
    }
];

const input2 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry'
    },
    {
        pos: 2,
        text: 'David'
    },
    {
        pos: 2,
        text: 'Edger'
    },
    {
        pos: 1,
        text: 'Lisa'
    },
];
const output2 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry',
        children: [{
                pos: 2,
                text: 'David'
            },
            {
                pos: 2,
                text: 'Edger'
            }
        ]
    },
    {
        pos: 1,
        text: 'Lisa'
    }
];

const input3 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry'
    },
    {
        pos: 2,
        text: 'David'
    },
    {
        pos: 3,
        text: 'Dexter'
    },
    {
        pos: 2,
        text: 'Edger'
    },
    {
        pos: 1,
        text: 'Lisa'
    },
];
const output3 = [{
        pos: 1,
        text: 'Andy'
    },
    {
        pos: 1,
        text: 'Harry',
        children: [{
                pos: 2,
                text: 'David',
                children: [{
                    pos: 3,
                    text: 'David'
                }]
            },
            {
                pos: 2,
                text: 'Edger'
            }
        ]
    },
    {
        pos: 1,
        text: 'Lisa'
    }
];

A comment explaining the approach along with a link to Github gist or code in comment will be appreciated. Bounties are handled by Steem Bounty

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi @gokulnk,

I ripped up a quick code with a nested function which takes care of any depth. I have tested my code against your inputs and outputs and it seems to be working fine. Check it out and leave me a comment in git if you find any issue.
https://github.com/mightypanda-x/flat-to-nested

·

I had not upvoted your comment because I was testing your code. I ran some fixed value tests and some random tests. Looks like your code is taking care of all the scenarios :)

Upvoted your answer with 100%. Unless I get a better solution, looks like you are going to be the winner of this Gig.

·
·

Glad it worked. I had fun solving it.

·

good job!

Love the dummy names =)

·

I used the names from one of the examples on a github and just added on to that :)

Just trying ro refresh my programming skills after a two year break. Sadly there are more questions for me than answers for you after trying a few minutes to help you out with this problem.

Will follow this maybe someone got an explanation I do understand to.

Resteemed it for more exposure!

·

Thanks for the resteem.

Holy crap that's a huge bounty. I can't contribute anything here for lack of necessary skills, but good luck. (No, I'm not expecting an upvote here; just expressing my admiration.)

·

I wanted to give Steem Bounty a try. Looks like it can get things done. Based on the output of this gig, I am planning to publish more gigs :)

Free Resteem For Steem-Bounty Users

  • Your Post Got Upvote
  • Your Post Resteemed To 800+ Followers
    How it Work You can Check Here

Untitled-2.png

·

Thanks for the resteem.

You want to convert your file from flat to nested, so here is a GitHub link as requested 😸 good luck with your project!

https://github.com/joaonuno/flat-to-nested-js/blob/master/README.md

·

This doesn't solve the problem in question directly. But it does solve one half of it. Thanks for the link.

I am not good at programming, sorry can't help you bud

The data is obtained from any source: file, database, content, or other form. Then you will have to make sure to define where you will get that information and in agreement you will need to do one thing or another. For example, if it is from a database, the structure creates it with a server language. if it is for a series of content that the user enters, he would have to provide several form controls and the possibility of adding several records. then with javascript you iterate the records and you are adding the information to the json creating the necessary properties.

This looks like reverse pre-order tree traversal. Maybe this post on Github could be of some help: https://gist.github.com/sabman/ac577c2f018290e169fa54ae1b0aafe5

Ok,
this is too much for my brain

·

It is for many programming brains as well ;) But this is a good problem to solve. I have been lazy and posed a Gig. Ideally I would have liked to spend time on this and come out with a solution. Now I will atleast analyse the solutions provided.

@gokulnk has set 11.000 STEEM bounty on this post! logo_for-light-bg_1000.png
What is a bounty exactly?

A bounty is money sent to a post to be distributed to the users commenting on it. It provides a way to reward users directly and works in addition to the steem/sbd they receive from the blockchain. It works independently of SteemPower.

You create a bounty by sending any amount of sbd/steem to @steem-bounty together with a post-url in the memo.

How can I earn a bounty Users are then competing for the bounty by writing their answers to the post in comments that will achieve upvotes from the community and especially the bounty creator. The money of the bounty gets distributed to all top level comments of the post at the same time when the post is paid out (7 Days after it was written). How much everyone gets depends on the votes the comments received. The sender of the bounties votes are weighted higher so that she decideds where 80% of the bounty money goes and all other votes determine the rest.

@steem-bounty does all of this for you automatically. You can use this service to automatically pay out a challenge, ask a hard question or simply to reward the people that interact with you.

Read more about how it works, even in different languages here.

·

Congratulations to the following winner(s) of the bounty!

  • @elleok has earned 0.086 STEEM. 0.075 STEEM from the creator of the bounty and 0.011 STEEM from the community!
  • @jmhb85 has earned 0.076 STEEM. 0.075 STEEM from the creator of the bounty and 0.001 STEEM from the community!
  • @tsnaks has earned 0.075 STEEM. 0.075 STEEM from the creator of the bounty and 0.001 STEEM from the community!
  • @malloryblythe has earned 0.084 STEEM. 0.075 STEEM from the creator of the bounty and 0.010 STEEM from the community!
  • @mow has earned 0.075 STEEM. 0.075 STEEM from the creator of the bounty and 0.000 STEEM from the community!
  • @crypticat has earned 0.379 STEEM. 0.373 STEEM from the creator of the bounty and 0.006 STEEM from the community!
  • @zorank has earned 0.075 STEEM. 0.075 STEEM from the creator of the bounty and 0.001 STEEM from the community!
  • @fresteem has earned 0.306 STEEM. 0.298 STEEM from the creator of the bounty and 0.007 STEEM from the community!
  • @sparkofphoenix has earned 0.091 STEEM. 0.075 STEEM from the creator of the bounty and 0.016 STEEM from the community!
  • @mightypanda has earned 9.599 STEEM. 7.458 STEEM from the creator of the bounty and 2.142 STEEM from the community!
  • @lanzjoseg has earned 0.001 STEEM. 0.000 STEEM from the creator of the bounty and 0.001 STEEM from the community!
  • @adalhelm has earned 0.077 STEEM. 0.075 STEEM from the creator of the bounty and 0.002 STEEM from the community!
  • @birapada has earned 0.075 STEEM. 0.075 STEEM from the creator of the bounty and 0.000 STEEM from the community!
  • @soumon has earned 0.002 STEEM. 0.000 STEEM from the creator of the bounty and 0.002 STEEM from the community!

Amigo creo que le falta hacer un cierre en el const output2 falta un ]

Wow I don't understand anything in this post ^^

I upvote it. Because I'n sure @gokulnk knows what he do ^^

As a newbie in the programming world,i think this is something i can learn greatly from,i cannot provide an answer but i will surely learn from answers provided by experienced programmers

·

You have got 25 votes for the comment. You must be really popular in the community. I will be checkout each of the answers and will post my reviews. Lets hope we can both learn things :)

·
·

All his comments have exactly 25 up-votes. He is just abusing the bounty system to get larger share.
https://steemit.com/@birapada/comments.

CC: @steem-bounty, @knircky

Hi @gokulnk,
No need to explain your code. It is really easy to understand because of your indentations make the code well organized.

What a nice use of Steem Bounty! Paying the solver of your problems with Steem Bounty. That's brillant! I will do the same if i have a problem in the future.

Hey man!!! Greetings!!