Underscore.js: take
Similar to tap() but changes the current object in the chaining. Or seen another way: similar to map() but on a single object.
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
JavaScript
var x = [{
"Book3" : "BULLETIN 3"
}, {
"Book1" : "BULLETIN 1"
}, {
"Book2" : "BULLETIN 2"
}];
function find_value(array, key) {
// find will run a function for every object in array
var obj_found = _.find(array, function(obj) {
// keys returns the keys inside an object
// so if the key of currently examined object
// is what we are looking for, return the obj
if (_.keys(obj)[0] === key) {
return obj;
}
});
// if an object with such key was found return its value
if (obj_found) {
return obj_found[key];
} else {
return null;
}
}
console.log(find_value(x, 'Book3'));