-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Actual “find” method #21
Comments
I will think about it ) |
Why not use find to return a reference instead of values? Since JS is all about "call-by-reference", one could easily modify those returned references to manipulate the original object. |
jspath returns reference to original object, but this object has no context with it parent/siblings. |
+1 for 'find' |
+1 |
+1, that would be nice to have |
+1 For me, I'd rather have a way to apply a function to the matching data and replace their values, i.e.,
Which would have the effect of changing
The existing name Just a thought. |
@xonegy Well in this case you would want to get the .automobiles object. Javascript returns Objects by reference, so if you set or delete a property of the object, it will update the original. Consider this example: https://gist.github.com/formula1/07d23b0e3842c7658f8d Here we only make modifications to res yet it changes j. As for getting a direct path from point A to point B, it could be nice. mpath is far less powerful than jspath, however it is an example of a direct path from point A to point B. Sample data "borrowed" from here: http://json.org/example If you want a cleaner jquery-esque interface, heres a wrapper Object https://gist.github.com/formula1/1c12acb2b21beb3943c7 it needs the JSPath to be in a variable names jspath and you'll want to create a new PathWrapper(".a..path{to your things}").apply(myOb).set("a-path", "a-value").delete("uglypath").get() == original values |
+1 for getting the path instead of the object or even both like in https://www.npmjs.com/package/jsonpath with jp.nodes would love to use jspath for that, because I really like the syntax. |
+1 I also need paths to found nodes, which JSONPath provides. Unfortunately JSONPath's query syntax can't filter by path and value simultaneously, so JSPath does better there. But without paths, JSPath is relatively useless for traversing the large JSON trees returned by parsers like Babylon. |
@dfilatov I went ahead and wrote what I thought was a way to index the json by value -> path so that paths could be looked up for JSPath results. It kind of works but only if every value in the json is a unique reference, which is unlikely in practice:
Output:
Live demo: https://repl.it/repls/SweetThickProcessors As you can see, if people really need the paths for results then there is really no way for them to get them themselves. It would be awesome if you could implement a call to return the results as something like
Where the path and original object arguments are optional, which is similar to the Array.map() prototype. Note that there is some consensus on JavaScript object paths as an array of keys, but converting that to a string is somewhat of an open question due to the existence of keys like Other than that, I think JSPath is pretty fantastic and so far is working better for me than JSONPath so thank you for your efforts. |
Right now I can see only
apply
method that returns the values themselves. However if I'd like to manipulate the data in some way, I'd need to know those items' locations.What I propose is to add something like
JSPath.find
method, which would get all the things you could throw to theapply
method:But this would return an array of the “steps” which lead to every found element, so in the case above the result would be:
This way you could then easily find any specific item or it's parent/siblings using any helper functions, so you then could change this item (like in #13) or change anything around it — its parents or siblings.
The text was updated successfully, but these errors were encountered: