keyfinder

Deep search for keys in objects and arrays and pluck their respective values.

Usage no npm install needed!

<script type="module">
  import keyfinder from 'https://cdn.skypack.dev/keyfinder';
</script>

README

keyfinder

NPM version Build Status Coverage Status Dependency Status

Deep search for keys in objects and arrays and pluck their respective values.

Install

$ npm install keyfinder --save
$ bower install keyfinder --save

Usage

keyfinder(object/array, "string")

Returns an array containing all the values of the keys that match the predicate.

var obj = {
  a: 'aa',
  b: 'bb',
  c: 'cc'
  d: {
    a: 'aaa'
  }
}

keyfinder(obj, 'a');
// → [ 'aa', 'aaa' ]

keyfinder(object/array, callback)

Iterates through the given object/array and calls the callback for every key/value pair.

// "obj" should be a JavaScript object or array.
keyfinder(obj, function(key, value, parent) {
  // key    = key or index depending on if the parent is an object or an array
  // value  = value of the key
  // parent = type of parent,  "object" or "array"
});

Tests

# clone this repo
$ npm install
$ npm test

License

MIT © Simon Johansson