array-find-by

Finds an element in an array of objects by attribute.

Usage no npm install needed!

<script type="module">
  import arrayFindBy from 'https://cdn.skypack.dev/array-find-by';
</script>

README

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 :tada:

License

ISC License