My objects:
[ { description: 'object1', id: 1 }, { description: 'object2', id: 2 } { description: 'object3', id: 3 } { description: 'object4', id: 4 } ]
In my function below, I pass a description to find the corresponding identifier:
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 be '1', but is undefined } }
I am trying to use the lodash search method: https://lodash.com/docs#find
However, my delete_id variable delete_id out undefined.
Update for people checking this question. Ramda is a good library that does the same thing that lodash does, but with a more functional way of programming :) http://ramdajs.com/0.21.0/docs/
javascript arrays lodash
Leon Gaban Jun 25 '15 at 15:02 2015-06-25 15:02
source share