简体   繁体   English

使用 lodash 从对象数组中获取值

[英]get the value from an array of object using lodash

i am new to lodash and Javascript .我是lodashJavascript lodash Here, I have following array of object,在这里,我有以下对象数组,

const bgStatus =
   [{
     Id: "809"
     Name: "PRE"
    Description: "PRE"
    Value: "VP:PRE"
    },
    {
     Id: "809"
     Name: "CLO"
    Description: "CLO"
    Value: "VP:CLO"
    },
    {
     Id: "809"
     Name: "BU"
    Description: "BU"
    Value: "VP:BU"
    }
    ]

Now, I want to get only description value if it matches with the value key.现在,如果它与值键匹配,我只想获得描述值。

So, I tried,所以,我试过,

currentStatus  = "VP:PRE"

Now,现在,

const obj =   _.find(buyingSessionStatus,{Value: this.currentStatus});

and when I did obj.Description then I am not getting the result which I was expecting to be PRE当我做obj.Description然后,我没有得到我期待就可以出结果PRE

Can any one help me with this ?谁能帮我这个 ?

Using Lodash:使用 Lodash:

const currentStatus = "VP:PRE";
const obj = _.find(bgStatus, status =>
                      currentStatus === status.Value)

Using Vanilla Javascript:使用香草 Javascript:

const currentStatus = "VP:PRE";
const obj = bgStatus.find(status => 
               currentStatus === status.Value);
console.log(obj.description); // "PRE"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM