
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
search-array
Advanced tools
Easy and lightweight search library for finding items inside an array of objects
Easy and lightweight search library for finding items inside an array of objects
You can use any of the below syntaxes to quey items inside arrays. A combination of these items is also possible.
[keywords]: Returns items that contain any of the keywords. Example, test, free text search"[keyword]": Returns items that contain the exact keyword. It preserves white spaces. Example, Back End-[keyword]: Items not containing the keyword.
-"Query with space"[key]:[keyword]: Returns items that have the [keyword] in the [key] property of the object. Example, name:bob
name:"Bob Rich" or, -name:bob (name should not contain bob)Installation:
npm i search-array
Usage:
const JsonSearch = require('search-array').default
const objectArray = [
{id:1, title: 'Good book', author: 'Jim', colors: 'red'},
{id:2, title: 'Interesting Movie', author: 'Bob', colors: 'dark red'},
{id:3, title: 'Good Series', author: 'Alex', colors: 'dark blue'},
{id:4, title: 'Something', author: 'Feri', colors: ['red', 'blue']}
]
const searcher = new JsonSearch(objectArray)
let foundObjects = searcher.query('good')
console.log(foundObjects) // prints items with id 1 and 3
foundObjects = searcher.query('good -red')
console.log(foundObjects) // prints item 3
foundObjects = searcher.query('good -colors:"dark blue"')
console.log(foundObjects) // prints item 1
foundObjects = searcher.query('red')
console.log(foundObjects) // prints item 1, 2 and 4
By default, the ordering of the original array is preserved in the results. But it's possible to sort results based on their match strength, example:
const searcher = new JsonSearch(objectArray, {
sort: true
})
So, the first item in the query() results would be a better match for the query compared to the last item in the results.
By default all object keys of the first item in the objectArray will be used for finding the items in the whole array. But you can specify search object keys by passing an object of indice as options. The syntax is as followings:
const searcher = new JsonSearch(objectArray, {
indice: {
'key used in query': 'corresponsding key in the object'
}
})
Example:
const JsonSearch = require('search-array').default
const objectArray = [
{id:1, title: 'Good book', author: 'Jim', colors: 'red'},
{id:2, title: 'Interesting Movie', author: 'Bob', colors: 'dark red'},
{id:3, title: 'Good Series', author: 'Alex', colors: 'dark blue'},
{id:4, title: 'Something', author: 'Feri', colors: ['red', 'blue']}
]
const searcher = new JsonSearch(objectArray, {
indice: {
'title': 'title', // search the `title`
'name': 'author' // search the `author` but it's renamed as `name` in queries
}
})
let foundObjects = searcher.query('good')
console.log(foundObjects) // prints items with id 1 and 3
let foundObjects = searcher.query('name:Jim')
console.log(foundObjects) // prints item with id 1
let foundObjects = searcher.query('author:Jim')
console.log(foundObjects) // finds nothing, the index `author` has not been defined in the options
foundObjects = searcher.query('red')
console.log(foundObjects) // finds nothing because the `red` does not exist in the 'title' or 'author' properties of items
Use as module:
<script type="module">
import JsonSearch from 'https://unpkg.com/search-array/dist/esm/min/JsonSearch.js'
const objectArray = [/*your objects here*/]
const searcher = new JsonSearch(objectArray)
let foundObjects = searcher.query('good')
</script>
Or, classic:
<script src="https://unpkg.com/search-array"></script>
<script>
// the JsonSearch class is available here
const objectArray = [/*your objects here*/]
const searcher = new JsonSearch(objectArray)
let foundObjects = searcher.query('good')
</script>
Installation:
npm i search-array
import JsonSearch from 'search-array'
...
FAQs
Easy and lightweight search library for finding items inside an array of objects
The npm package search-array receives a total of 265 weekly downloads. As such, search-array popularity was classified as not popular.
We found that search-array demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.