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.

4
  • Is there a reason you have to use lodash? There are plenty of ways of doing this in plain old JavaScript Commented Feb 1, 2017 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? Commented Feb 1, 2017 at 18:00
  • content.some(c => (c.set_archived || c.something.some(s => s.archived))) Commented Feb 1, 2017 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. Commented Feb 1, 2017 at 18:25

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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().

Comments

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)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.