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.
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
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 :)
How it Work You can Check Here
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.
Wow I don't understand anything in this post ^^
I upvote it. Because I'n sure @gokulnk knows what he do ^^
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.
No need to explain your code. It is really easy to understand because of your indentations make the code well organized.Hi @gokulnk,
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.
This looks like reverse pre-order tree traversal. Maybe this post on Github could be of some help:
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
Amigo creo que le falta hacer un cierre en el const output2 falta un ]
I am not good at programming, sorry can't help you bud
Deivid või Daweed?
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.@gokulnk has set 11.000 STEEM bounty on this post!@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!
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.
My approach uses recursion Hi @gokulnk
Hey man!!! Greetings!!