// code (that's all):
var mjp = (p, o, n) => p.split(/\.|\[|\]/).filter((x) => !!x).reduce((a, c, i, l) => l.length-i <= n ? a : a[c], o);
// usage:
let obj = {
"x": 1,
"y": [
[
{
"z": 4
}
],
[
{
"z": 6
}
]
]
};
let path = 'y[0][0].z';
let nestedValue = mjp(path, obj); // 4
// You can set a third argument to get the nth parent from the path
let nestedValueFirstParent = mjp(path, obj, 1); // {z: 4}
let nestedValueSecondParent = mjp(path, obj, 2); // [{"z":4}]
-
Notifications
You must be signed in to change notification settings - Fork 0
License
arturaugusto/miniJSONPath
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description or website provided.
Topics
Resources
License
Stars
Watchers
Forks
Releases
No releases published