@gizt/selector
is a fast and intuitive JSON selector (the syntax is based on JSON notation and glob).
You can try it online here playground
npm install @gizt/selector
// es6/browser
import select from '@gizt/selector'
// node
const select = require('@gizt/selector')
let data = {
"title": "Awesome",
"users": [
{ "name": "John", "family": "Doe", "friends": [{ "name": "F1" }] },
{ "name": "Joe", "family": "Dae", "friends": [{ "name": "F2" }] }
...
]
}
Simple property/name selector
// Property selector
select('users[].name', data) // ['John', 'Joe', ...]
// Nested array
select('users[].friends[].name', data) // ['F1', 'F2']
Support prefix, suffix, middle and star
// prefix
select('users[].na*', data) // ['John', 'Joe']
// suffix
select('users[].*ly', data) // ['Doe', 'Dae']
// middle
select('users[].*am*', data) // ['John', 'Doe', 'Joe', 'Dae']
// star
select('users[].friends[].*', data) // ['F1', 'F2']
Array index & slice
// index
select('users[1].name', data) // 'Joe'
// all elements start from index `1` same as `data.slice(1)`
select('users[1:].name', data) // ['Joe', 'Ja', ...]
// first 2 elements same a `data.slice(0, 2)`
select('users[:2].name', data) // ['John', 'Joe']
Also support array input
select('[].name', [ { name: 'a'}, { name: 'b' } ]) // ['a', 'b']
For more example, please see test
directory