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

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

My objects:

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


        
10条回答
  •  粉色の甜心
    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;
            },
    

提交回复
热议问题