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

前端 未结 10 1109
终归单人心
终归单人心 2020-11-29 18:58

My objects:

[
    {
        description: \'object1\', id: 1
    },
    {
        description: \'object2\', id: 2
    }
    {
        description: \'object3\         


        
10条回答
  •  天命终不由人
    2020-11-29 19:52

    for this find the given Object in an Array, a basic usage example of _.find

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

    this would work well

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

    _.find will help with returning an element in an array, rather than it’s index. So if you have an array of objects and you want to find a single object in the array by a certain key value pare _.find is the right tools for the job.

提交回复
热议问题