1

This is my Array (http://www.jsoneditoronline.org/?id=cc51a12581667055781639b586fa6e15):

[
  {
    "documents": [
      {
        "name": "a",
        "isSelected": true,
        "status": "good"
      },
      {
        "name": "b",
        "isSelected": false,
        "status": "good"
      }
    ]
  },
  {
    "documents": [
      {
        "name": "a",
        "isSelected": true,
        "status": "bad"
      },
      {
        "name": "b",
        "isSelected": false,
        "status": "good"
      }
    ]
  },
  {
    "documents": [
      {
        "name": "a",
        "isSelected": true,
        "status": "verygood"
      },
      {
        "name": "b",
        "isSelected": false,
        "status": "good"
      }
    ]
  },
  {
    "documents": [
      {
        "name": "a",
        "isSelected": false,
        "status": "good"
      },
      {
        "name": "b",
        "isSelected": false,
        "status": "good"
      }
    ]
  }
]

I need to write condition using _.lodash. This condition has to return TRUE if in array is at least one Selected document with Status other than good or verygood


Based on array from above. http://prnt.sc/de3gx9 You can see on the screenshot that in array is an object with:

  1. isSelected
  2. Has other status than good or verygood

If in my Array is at least one object (with isSelected = true, and status = bad (or any other than good or verygood). Then I want to see RESULT: TRUE

 function checkStatusInArray() {   
       var data = [....]; // this is my array

       var isDocumentSelectedWithWrongStatus = _.some(data, { isSelected: true, status: !"good" || !"verygood" });
       // So if in array are some items with isSelected = true and status != good || verygood, then isDocumentSelectedWithWrongStatus = TRUE

       return isDocumentSelectedWithWrongStatus; 
    }
4
  • Can you clarify the condition to filter? Commented Dec 1, 2016 at 15:04
  • I added filter, because I thought that gonna be easier with less number of objects Commented Dec 1, 2016 at 15:09
  • I want to see only bool reasult. If there will be item with isSelected = true and status = bad, worst, badly or any except: good or verygood. Commented Dec 1, 2016 at 16:00
  • Please check code from edit. Commented Dec 1, 2016 at 16:07

3 Answers 3

2

You can do it without lodash - Array#some inside Array#some and a predicate (the predicate can be inlined as anonymous function), if the predicate returns, the result will be truthy:

function checkData(data, predicate) {
  return data.some(function(item) {
    return item.documents.some(predicate);
  });
}

function predicate(document) {
  return document.isSelected && 
    !(document.status === 'good' ||
    document.status === 'verygood');
}

function checkData(data, predicate) {
  return data.some(function(item) {
    return item.documents.some(predicate);
  });
}

function predicate(document) {
  return document.isSelected && 
    !(document.status === 'good' ||
    document.status === 'verygood');
}

var data = [{
  "documents": [{
    "name": "a",
    "isSelected": true,
    "status": "good"
  }, {
    "name": "b",
    "isSelected": false,
    "status": "good"
  }]
}, {
  "documents": [{
    "name": "a",
    "isSelected": true,
    "status": "bad"
  }, {
    "name": "b",
    "isSelected": false,
    "status": "good"
  }]
}, {
  "documents": [{
    "name": "a",
    "isSelected": true,
    "status": "verygood"
  }, {
    "name": "b",
    "isSelected": false,
    "status": "good"
  }]
}, {
  "documents": [{
    "name": "a",
    "isSelected": false,
    "status": "good"
  }, {
    "name": "b",
    "isSelected": false,
    "status": "good"
  }]
}];

var result = checkData(data, predicate);

console.log(result);

Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure that is correctly? It seems that it doesn't work properly.
Well when I was trying to use it in this way: var b = deepFilter(data, predicate); where data = my array, then this function deepFilter assigned to var b all arrays.
Code snippet returns 3 objects. I wanted to check whether in Data is selected document with status other than good or verygood. If there is at least one. I wanted to see TRUE. Nothing more.
Maybe this filter was a bad approach. Anyway this filter return me objects where isSelected=true So only first 3 records of Array in this case. However it give me nothing. Because there are still documents with status other than good or verygood. I'll try to explain you in edit.
1

You could use lodash's flatMap to pluck the documents into one collection and then use some on the collection:

var data = [{"documents":[{"name":"a","isSelected":true,"status":"good"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":true,"status":"bad"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":true,"status":"verygood"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":false,"status":"good"},{"name":"b","isSelected":false,"status":"good"}]}]

var selectedButNotGood = function(document){
  return document.isSelected && 
    document.status != "good" && 
    document.status != "verygood"
}

var result = _.chain(data)
              .flatMap('documents')
              .some(selectedButNotGood)
              .value();

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

Comments

0

You can loop over them and create your own custom array.

var d = [{"documents":[{"name":"a","isSelected":true,"status":"good"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":true,"status":"bad"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":true,"status":"verygood"},{"name":"b","isSelected":false,"status":"good"}]},{"documents":[{"name":"a","isSelected":false,"status":"good"},{"name":"b","isSelected":false,"status":"good"}]}]

var statusList = ['good', 'verygood']
var r = d.some(function(o) {
  return o.documents.filter(x => !statusList.includes(x.status) && x.isSelected).length > 0;
});
console.log(r)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.