javascript - How to use lodash to find and return an object from Array? -
my objects:
[ { description: 'object1', id: 1 }, { description: 'object2', id: 2 } { description: 'object3', id: 3 } { description: 'object4', id: 4 } ]
in function below i'm passing in description find matching id:
function plucksavedview(action, view) { console.log('action: ', action); console.log('plucksavedview: ', view); // view = 'object1' var savedviews = retrievesavedviews(); console.log('savedviews: ', savedviews); if (action === 'delete') { var delete_id = _.result(_.find(savedviews, function(description) { return description === view; }), 'id'); console.log('delete_id: ', delete_id); // should '1', undefined } }
i'm trying use lodash's find method: https://lodash.com/docs#find
however variable delete_id
coming out undefined.
update people checking question out, ramda nice library samething lodash does, in more functional programming way :) http://ramdajs.com/0.21.0/docs/
the argument passed callback 1 of elements of array. elements of array objects of form {description: ..., id: ...}
.
var delete_id = _.result(_.find(savedviews, function(obj) { return obj.description === view; }), 'id');
yet alternative docs linked to:
_.find(savedviews, 'description', view);
Comments
Post a Comment