This searcher checks the javascript object elements of an array and search for string match, even in nested arrays inside javascript objects, and retreieves the original array elements that matches with the string parameter.
# Using npm
npm i nested-javascript-search
At first import the library to your project.
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.
<script type="module">
import { search } from 'https://unpkg.com/nested-javascript-search@latest/dist/index.js'
// Then you can use search as a function here.
</script>
Then you can call to the search function.
search(myArray, myStringToMatch)
Filter the array elements by searching into the nested array. It will then return the main array element due to the nested string match.
import { search } from 'https://unpkg.com/nested-javascript-search@latest/dist/index.js'
const myArray = [
{
id:0,
name:'foo',
nested: {
name:'cake'
}
},
{
id:1,
name:'bar',
nestedArray:[
{name:'coffee'}
]
},
]
console.log(search(myArray,'coffee')) // [ { id: 1, name: 'bar', nestedArray: [ [Object] ] } ]
console.log(search(myArray,'foo')) // [ { id: 0, name: 'foo', nested: { name: 'cake' } } ]
console.log(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.
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.
import { search } from 'https://unpkg.com/nested-javascript-search@latest/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' } ]
This also works on nested properties keys.