How to use lodash to find and return an object from an array?

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/

+131
javascript arrays lodash
Jun 25 '15 at 15:02
source share
11 answers

The argument passed to the callback is one of the elements of the array. The elements of your array are objects of the form {description:..., id:...} .

 var delete_id = _.result(_.find(savedViews, function(obj) { return obj.description === view; }), 'id'); 

Another alternative from the documents you are associated with (lodash v3):

 _.find(savedViews, 'description', view); 

Lodash v4:

 _.find(savedViews, ['description', view]); 
+155
Jun 25 '15 at 15:04
source share

lodash and ES5

 var song = _.find(songs, {id:id}); 

lodash and ES6

 let song = _.find(songs, {id}); 

docs at https://lodash.com/docs#find

+181
Jul 26 '16 at 4:48
source share

You can do it easily in vanilla JS:

Using find

 const savedViews = [{"description":"object1","id":1},{"description":"object2","id":2},{"description":"object3","id":3},{"description":"object4","id":4}]; const view = 'object2'; const delete_id = savedViews.find(obj => { return obj.description === view; }).id; console.log(delete_id); 

Using filter (original answer)

 const savedViews = [{"description":"object1","id":1},{"description":"object2","id":2},{"description":"object3","id":3},{"description":"object4","id":4}]; const view = 'object2'; const delete_id = savedViews.filter(function (el) { return el.description === view; })[0].id; console.log(delete_id); 

+26
Jun 25 '15 at 15:07
source share

Using the find method, your callback will pass the value of each element, for example:

 { description: 'object1', id: 1 } 

So you need a code like:

 _.find(savedViews, function(o) { return o.description === view; }) 
+11
Jun 25 '15 at 15:05
source share
 var delete_id = _(savedViews).where({ description : view }).get('0.id') 
+6
Jun 25 '15 at 15:07
source share

You do not need Lodash, Ramda or any other additional addiction.

Just use the ES6 find () function functionally:

 savedViews.find(el => el.description === view) 

Sometimes you need to use third-party libraries to get all the goodies that come with them. However, generally speaking, try to avoid dependencies when you don't need them . Dependencies may:

  • inflate your code size in the kit,
  • You will need to keep them informed,
  • and they may pose errors or security risks
+6
Nov 04 '18 at 6:52
source share

to do this, find the given object in the array, the basic example of using _.find

 const array = [ { description: 'object1', id: 1 }, { description: 'object2', id: 2 }, { description: 'object3', id: 3 }, { description: 'object4', id: 4 } ]; 

it will work well

 q = _.find(array, {id:'4'}); // delete id console.log(q); // {description: 'object4', id: 4} 

_.find will help return the element in the array, not its index. So if you have an array of objects, and you want to find a single object in the array by a specific key value, pare _.find is a suitable tool for working.

+6
Mar 25 '19 at 13:02
source share

You can use the following

 import { find } from 'lodash' 

Then return the entire object (not only its key or value) from the list with the following:

 let match = find(savedViews, { 'ID': 'id to match'}); 
+5
Jun 03 '18 at 17:23
source share

Import lodash using

$ npm i --save lodash

 var _ = require('lodash'); var objArrayList = [ { name: "user1"}, { name: "user2"}, { name: "user2"} ]; var Obj = _.find(objArrayList, { name: "user2" }); // Obj ==> { name: "user2"} 
+2
Jul 31. '19 at 10:19
source share

let labArray = [{id: 1, name: 'user1'}, {id: 2, name: 'user1'}, {id: 3, name: 'user1'}]; let selectedLabId = 2; let selectedLabInfo = _.find (labArray, {'id': parseInt (selectedLabId)});

0
Jun 12 '19 at 10:49 on
source share

Get id based on name

  { "roles": [ { "id": 1, "name": "admin", }, { "id": 3, "name": "manager", } ] } fetchIdBasingOnRole() { const self = this; if (this.employee.roles) { var roleid = _.result( _.find(this.getRoles, function(obj) { return obj.name === self.employee.roles; }), "id" ); } return roleid; }, 
0
Jun 19 '19 at 6:16
source share



All Articles