• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to add the child node to the corresponding parent node in JSON?

 
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I've got an array that contains the first level of items. For example:



Each one of them got an ID. To get that data I use graphql query:



As you can see it uses Lodash for array manipulation.

Now, I've got the items that belong to a specific folder. It is using another graphql query that gets the files for a particular folder based on the parent ID. Have a look:



As you can see, I've got two queries doing two jobs. 1st is to get the first level of items and the second is returning the items belonging to a particular folder based on the parent ID of the folder.

So eventually, I get something like that:



The thing I try to achieve is, I want to make all these structures as a single JS object:



The idea is I have at the moment 2 arrays. One for the first level of items and the second array is the child node containing the files to each parent folder.

How can combine these two arrays so I build the structure shown above?

The main developer of this project had used Lodash for that. Is there a way to accomplish that? Or it can be done with pure Javascript?
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites.

https://stackoverflow.com/questions/64118751/how-to-add-the-child-node-to-the-corresponding-parent-node-in-json

I don't really know any of the libraries you're using, but this Graph Query Language suggests to me that you can get the structure you want by just making the initial query one level deeper. Can't you add a property to your tree field so that it will return the children of a specific folder in addition to the folder's other properties?

If you want this to work for an arbitrary depth and not just two levels, then you need to write a recursive function. You could do something like this:

This prints:
 
Marin Capranov
Ranch Hand
Posts: 69
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the thing is I can go deeper than the second level. Is just me that I need that only. The problem is that I am working on a open source project and I am afraid that would be hard to implement your code.

The project is called Wiki JS.

Regarding the libraries, I can say that it uses Vue-apollo for queries and Lodash library for array and collection handling.

If you look at the parameters of the queries you can see the variable $parent. That one is responsible for finding the parent folders and checks whether there are any sub folder, files or not.

For ex: parent: 1, locale: this.locale will return the files if any that have the parent id as 1, and so it works to any other level. If a folder is as deep as third level and its parent ids would be, for example, 1 -> 2 - 3(our file) than in order to get that file we will have to assign 3 to the parent variable.

I ran into a problem that the parent variable accepts only one value, eventually will return only the items of on parent folder. I try now to see how to return all the files under any parent folder.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic