Array.prototype.findBy
Finds an element in an array of objects by attribute.
Install
npm install array-find-by --save
Usage
Import it:
let planets = id: 1 name: 'Saturn' id: 2 name: 'Jupiter' id: 3 name: 'Uranus'
Call it...
let jupiter index = findBy // jupiter -> {id: 2, name: 'Jupiter'} // index -> 1
...or add it on the prototype:
if !ArrayprototypefindBy ArrayprototypefindBy = findBy let jupiter index = planets // jupiter -> {id: 2, name: 'Jupiter'} // index -> 1
Changelog
-
2.0.0
- It returns now the index too.
-
1.0.0
- Initial release 🎉