简体   繁体   English

使用Lodash _.find根据属性从数组获取对象

[英]Get object from array based on property using Lodash _.find

I have an array of objects, say memberToChange.checkboxes: ICheckbox[] like this: 我有一个对象数组,例如memberToChange.checkboxes:ICheckbox []像这样:

在此处输入图片说明

Now, I have a variable, say internalNumber: string which has the value "3419". 现在,我有一个变量,例如internalNumber:字符串,其值为“ 3419”。 I want to get the object from the array of objects where the internalNumber matches the label property. 我想从internalNumber与label属性匹配的对象数组中获取对象。 Ultimately, I want to set the value attribute of that object to true. 最终,我想将该对象的value属性设置为true。

My code is: 我的代码是:

let checkboxes = _.find(scope.selectedMembers, (member: IMember) => member.member.uuid === memberId).checkboxes;     //gives me array of checkboxes.  
let checkboxToChange = _.find(memberToChange.checkboxes, function(checkbox: ICheckbox){
  return (checkbox.label === internalNumber);
}); //gives me null, moreover, even a console.log inside the second find doesn't print. I'm thinking the two consecutive _.find statements are messing something up, not sure what.

For reference, this is my ICheckbox interface: 供参考,这是我的ICheckbox界面:

export interface ICheckbox {
    label: string;
    sublabel?: string;
    value: boolean;
    numberUuid: string;
}

I would expect that for internalNumber 3419, it should return me the second object from the array. 我希望对于internalNumber 3419,它应该向我返回数组中的第二个对象。 But it returns undefined. 但是它返回未定义的。 I'm not sure what's going on here. 我不确定这是怎么回事。 If there is a better way to find and set the value to true in one go only, I'd be happy to know that as well. 如果有更好的方法可以一次性查找并将值设置为true,我也很高兴知道这一点。 Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

Update: 更新:

After someone suggested using filter method of javascript, I tried this: (my scope is assigned to this) 在有人建议使用javascript的筛选器方法后,我尝试了此操作:(我的作用域已分配给它)

scope.selectedMembers.filter(function(member) {
    if (member.member.uuid === memberId) {
      scope.memberCheckboxes = [];
      console.log('found member'); //prints 
      scope.memberCheckboxes = member.checkboxes;
      console.log(scope.memberCheckboxes); // print correctly, with checkboxes of the member
      scope.memberCheckboxes.filter(function(checkbox) {
        console.log('inside checkbox function'); //control doesnt even come here
        if (checkbox.label === intNum) {
          console.log('found checkbox'); // control doesnt come here 
        }
      });
    }
  });

Here, I don't understand why the first console.log inside scope.memberCheckboxes.filter doesn't print? 在这里,我不明白为什么scope.memberCheckboxes.filter内部的第一个console.log无法打印? Am I missing something obvious here? 我在这里错过明显的东西吗?

By some reason your memberToChange.checkboxes (or member.checkboxes in your updated question) have no elements. 由于某种原因,您的memberToChange.checkboxes (或更新的问题中的member.checkboxes )没有任何元素。

It is the only explanation why it does not work since your code is otherwise correct. 这是为什么它不起作用的唯一解释,因为您的代码是正确的。 The fact that console.log('inside checkbox function') does not print confirms that. console.log('inside checkbox function')无法打印的事实证实了这一点。

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

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