2

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?

So in this case, because the second object has set_is_archived that is true, then the expected response should be true. If all items are false in either object, then the response should be false.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
webbydevy
  • 1,200
  • 3
  • 15
  • 21
  • Is there a reason you have to use lodash? There are plenty of [ways of doing this in plain old JavaScript](http://stackoverflow.com/q/8217419/215552) – Heretic Monkey Feb 01 '17 at 17:59
  • 1
    Possible duplicate of [Can \_lodash test an array to check if an array element has a field with a certain value?](http://stackoverflow.com/questions/21278661/can-lodash-test-an-array-to-check-if-an-array-element-has-a-field-with-a-certai) – Heretic Monkey Feb 01 '17 at 18:00
  • `content.some(c => (c.set_archived || c.something.some(s => s.archived)))` – Andreas Feb 01 '17 at 18:04
  • `something.archived` is invalid with the given structure. What is `set_is_archived`? Do you mean `set_archived`? Be consistent. Also, most Lodash questions are similar to this one, search Stack Overflow (and the web) before asking. Look through the great Lodash documentation which have examples for each function. – Emile Bergeron Feb 01 '17 at 18:25

4 Answers4

6

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:

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

or

 content.filter(o => o["set_archived"] || o.something[0].archived).length > 0;
cyr_x
  • 13,987
  • 2
  • 32
  • 46
1

You can just use some() in plain javascript.

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)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

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. (I could have said vice-versa)

The some() method tests whether some element in the array passes the test implemented by the provided function.

The every() method tests whether all elements in the array pass the test implemented by the provided function.

If you are looking for a stricter way I recon you go ahead with every().

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26
0

You don't need lodash for this. You can do this with pure JS, using filter:

content.filter(i => i.set_archived || i.something.archied)
Kousha
  • 32,871
  • 51
  • 172
  • 296