1.0.0 • Published 10 years ago

keyfinder v1.0.0

Weekly downloads
691
License
MIT
Repository
github
Last release
10 years ago

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