Javascript Array deep search filter.
npm install nested-javascript-searchbash
Using npm
npm i nested-javascript-search
`
Usage
At first import the library to your project.
`JavaScript
import { search } from 'nested-javascript-search'
// or by using a CDN.
import { search } from 'https://unpkg.com/nested-javascript-search@latest/dist/index.js'
`
You can also use it in your html using a script tag with "module" as type.
`html
`
Then you can call to the search function.
`JavaScript
search(myArray, myStringToMatch)
`
Example
Filter the array elements by searching into the nested array. It will then return the main array element due to the nested string match.
`JavaScript
const searcher = require('../dist/index.js')
const myArray = [
{
id:0,
name:'foo',
nested: {
name:'cake'
}
},
{
id:1,
name:'bar',
nestedArray:[
{name:'coffee'}
]
},
]
console.log(searcher.search(myArray,'coffee')) // [ { id: 1, name: 'bar', nestedArray: [ [Object] ] } ]
console.log(searcher.search(myArray,'foo')) // [ { id: 0, name: 'foo', nested: { name: 'cake' } } ]
console.log(searcher.search(myArray,'cake')) // [] This version only search inside arrays of objects.
`
This searcher only works with arrays, so it wont work with values as nested objects.
$3
You can also set a blacklist for searching. The blacklist must be an array of strings. Blacklisted elements will be ignored from the keys of the javascript objects during the search.
`JavaScript
const searcher = require('../dist/index.js')
const myArray = [
{
id:0,
name:'foo',
lastname: 'bar'
},
{
id:1,
name:'bar',
lastname: 'footer'
},
]
const blacklist = [
'name'
]
// It wont check keys called "example_name", so it will retrieve
// the object that its property "lastame" is "footer",
// since "footer" matches with "foo".
console.log(search(myArray,'foo', blacklist)) // [ { id: 1, example_name: 'bar', lastname: 'footer' } ]
``