1.0.0 • Published 8 years ago
find-by-key v1.0.0
Find by Key
Find objects in deeply nested object trees.
Install
$ npm install find-by-key --save
Usage
var findByKey = require('find-by-key');
var tree = {
public: {
serviceList: [
'pub-service-1',
'pub-service-2'
]
},
devices: [{
device: {
serviceList: [
'a-service-1',
'a-service-2',
'a-service-3',
'a-service-4'
]
},
},{
device: {
serviceList: [
'b-service-1',
'b-service-2',
'b-service-3'
]
}
}]
}
var serviceLists = findByKey(tree, 'serviceList');
// Result
[
["pub-service-1", "pub-service-2"],
["a-service-1", "a-service-2", "a-service-3", "a-service-4"],
["b-service-1", "b-service-2", "b-service-3"]
]
Methods
/**
* findByKey
*
* @param {Object} nested Object to search
* @param {String} needle Key to find
* @return {Array}
*/
findByKey(nested, needle);
Legal
Original is taken from http://stackoverflow.com/a/10948908/294076
1.0.0
8 years ago