hbi99 / defiant.js

http://defiantjs.com
GNU Affero General Public License v3.0
913 stars 91 forks source link

find parent using recursion #125

Closed Scnck closed 3 years ago

Scnck commented 3 years ago

what is the best method of finding a parent object based on "key" rather than /.. for one level my results maybe returned from different object levels... be I want to return the main root parent.

hbi99 commented 3 years ago

Hi @Scnck, There are at least two ways to do this (that I know of). The examples below uses the XPath Evaluator at https://www.defiantjs.com/ The first one is this - enter the following XPath in the input field:

//store[.//isbn]/bicycle

...this "tells" that we are looking for the store node and make a sub-query with "[...]". In this case I added that I want to make further search with store as "base".

The second one is this:

//@price[ancestor::book//category = "fiction"]

...this "tells" that we want @price but only if it's ancestor is "book" and @price are among the one with category of "fiction". With "//", the query is looking for matches that is regardless of "depth" - meaning "category" and "@price" does not need to be on the same level in depth if the data structure.

I hope this helps. I would like to suggest that you cut and paste part of your JSON data in to the Evaluator by clicking the "pen". This way you can experiment your way to the right query you are looking for. I am closing this issue - re-open it, if you need further assistance.