question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get the path matching with the dot path syntax ?

See original GitHub issue

Hi, I would like to know if it would be possible, when working with arrays, to get the path that matched with the condition.

const data = { a: [{ b: 1 }, { b: 2 }] };

const result = sift({ "a.b": 1 }, data);
// --> a[0].b

Would it be feasible with a custom operator or is there a place where I can grab that info ?

Thank you

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
crcncommented, Nov 5, 2020

Closing this for now since @stalniy seems to have a good fix.

0reactions
stalniycommented, Aug 5, 2020

Hi @J3m5

In terms of ucast, you need:

  • MongoQueryParser - implemented in @ucast/mongo
  • operators from JsInterpreter - implemented in @ucast/js
  • custom js interpreter with logic to aggregate matching path instead of aggregating resulting value.

If you want to learn more, start from here

This is not something I plan to add ucast at least for now, because its main goal is to translate conditions from language to another (e.g., mongo query -> SQL, query string -> Mongo query, etc)

Details: theoretically it’s possible to do. Currently, @ucast/mongo provides a MongoQueryParser class, which you can use to parse any mongo query into AST (a tree of objects, you can think of it as something similar to XML tree). Having these objects, you can iterate over them to “interpret” them in anyway you need (e.g., XML can be interpreted as RSS, data transfer object or HTML).

So, during iteration you can use operator interpreters from @ucast/js (e.g., $lt, $gt, $in, $eq`, etc) to get boolean result, also you need to aggregate path down the tree. As soon as operator returns true for your desired case, just stop iteration and return aggregated path.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSONPath Syntax when dot in key - Stack Overflow
I get 'No match'. If I change the key to "footernavigationLinks" it works but I cannot control the key names in the JSON...
Read more >
JSONPath Syntax | AlertSite Documentation
A JSONPath expression specifies a path to an element (or a set of elements) in a JSON structure. Paths can use the dot...
Read more >
File path formats on Windows systems | Microsoft Learn
In this article, learn about file path formats on Windows systems, such as traditional DOS paths, DOS device paths, and universal naming ...
Read more >
dot-path - npm search
Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other...
Read more >
Path Operations - Essential Java Classes - Oracle Help Center
You can easily create a Path object by using one of the following get methods from the Paths (note the plural) helper class:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found