array-find-by

2.0.3 • Public • Published

Array.prototype.findBy

Finds an element in an array of objects by attribute.

Install

npm install array-find-by --save

Usage

Import it:

    import findBy from 'array-find-by'
    
    let planets = [{id: 1, name: 'Saturn'}, {id: 2, name: 'Jupiter'}, {id: 3, name: 'Uranus'}]
 

Call it...

    let [jupiter, index] = findBy.call(planets, 'id', 2)
    // jupiter -> {id: 2, name: 'Jupiter'}
    // index   -> 1

...or add it on the prototype:

    if (!Array.prototype.findBy) {
        Array.prototype.findBy = findBy
    }
 
    let [jupiter, index] = planets.findBy('id', 2)
    // jupiter -> {id: 2, name: 'Jupiter'}
    // index   -> 1

Changelog

  • 2.0.0

    • It returns now the index too.
  • 1.0.0

    • Initial release 🎉

License

ISC License

Readme

Keywords

none

Package Sidebar

Install

npm i array-find-by

Weekly Downloads

5

Version

2.0.3

License

ISC

Last publish

Collaborators

  • educastellano