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

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

My objects:

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


        
相关标签:
10条回答
  • 2020-11-29 19:36

    You can use the following

    import { find } from 'lodash'
    

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

    let match = find(savedViews, { 'ID': 'id to match'});
    
    0 讨论(0)
  • 2020-11-29 19:37

    You can do this 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);

    0 讨论(0)
  • 2020-11-29 19:43

    With the find method, your callback is going to be passed the value of each element, like:

    {
        description: 'object1', id: 1
    }
    

    Thus, you want code like:

    _.find(savedViews, function(o) {
            return o.description === view;
    })
    
    0 讨论(0)
  • 2020-11-29 19:43

    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"}
    
    0 讨论(0)
  • 2020-11-29 19:43

    Fetch id basing 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 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题