简体   繁体   English

检查lodash中的对象数组的属性值

[英]Check array of objects in lodash for property value

Given the following 鉴于以下

var content = [{
        "set_archived": false,
        "something": [{
            "id": "aa7bb3db-19a2-4ef6-5944-892edaaf53c3",
            "modified": "2016-12-01T18:23:29.743333Z",
            "created": "2016-12-01T18:23:29.743333Z",
            "archived": false
        }]
    },
    {
        "set_archived": true,
        "something": [{
            "id": "aa7bb3db-19a2-4ef6-5944-892edaaf53c3",
            "modified": "2017-01-30T19:42:29.743333Z",
            "created": "2017-01-30T19:42:29.743333Z",
            "archived": false
        }]
    }
];

Using Lodash, how would I determine if either set_archived or something.archived in the array of objects is equal to true? 使用Lodash,我如何确定对象数组中的set_archivedsomething.archived等于true?

So in this case, because the second object has set_is_archived that is true, then the expected response should be true. 因此,在这种情况下,由于第二个对象的set_is_archived为true,因此预期响应应该为true。 If all items are false in either object, then the response should be false. 如果在任何一个对象中所有项目均为假,则响应应为假。

Just use: 只需使用:

_.filter(content, o => o["set_archived"] || o.something[0].archived).length > 0;

or 要么

_.some(content, o => o["set_archived"] || o.something[0].archived);

PlainJs: PlainJs:

content.some(o => o["set_archived"] || o.something[0].archived)

or 要么

 content.filter(o => o["set_archived"] || o.something[0].archived).length > 0;

You can just use some() in plain javascript. 您可以在纯JavaScript中使用some()

 var content = [{ "set_archived": false, "something": [{ "id": "aa7bb3db-19a2-4ef6-5944-892edaaf53c3", "modified": "2016-12-01T18:23:29.743333Z", "created": "2016-12-01T18:23:29.743333Z", "archived": false }] }, { "set_archived": true, "something": [{ "id": "aa7bb3db-19a2-4ef6-5944-892edaaf53c3", "modified": "2017-01-30T19:42:29.743333Z", "created": "2017-01-30T19:42:29.743333Z", "archived": false }] }]; var result = content.some(function(e) { return e.set_archived === true || e.something[0].archived === true }) console.log(result) 

You don't need lodash for this. 您不需要lodash。 You can do this with pure JS, using filter : 您可以使用filter使用纯JS来做到这一点:

content.filter(i => i.set_archived || i.something.archied)

Considering the same object as the one in the question, there are couple of ways to find the solution to your problem mate. 考虑到与问题中的对象相同的对象,有两种方法可以找到问题伴侣的解决方案。

var flag = false;
flag = content.some(function(c){
    if(c["set_archived"]){
    return true;
  }
});

console.log(flag)

or 要么

    var flag = false;
    flag = content.some(function(c){
        if(c["set_archived"] || c.something[0].archived){
        return true;
      }
    });

    console.log(flag)

The above snippet will result true if atleast one of the object of the array have ["set_archived"] property true and it will return false if all the objects of the array has ["set_archived"] as false. 如果该数组的至少一个对象具有[“ set_archived”]属性为true,则上述代码片段将为true;如果该数组的所有对象都具有[“ set_archived”]为false,则上述代码段将返回false。 (I could have said vice-versa) (反之亦然)

The some() method tests whether some element in the array passes the test implemented by the provided function. some()方法测试数组中的某些元素是否通过提供的函数实现的测试。

The every() method tests whether all elements in the array pass the test implemented by the provided function. every()方法测试数组中的所有元素是否通过提供的函数实现的测试。

If you are looking for a stricter way I recon you go ahead with every(). 如果您正在寻找更严格的方法,我建议您继续使用every()。

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

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