简体   繁体   English

如何使用Lodash过滤嵌套数组?

[英]How to filter nested array using Lodash?

This is my Array ( http://www.jsoneditoronline.org/?id=cc51a12581667055781639b586fa6e15 ): 这是我的数组( 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. 我需要使用_.lodash写条件。 This condition has to return TRUE if in array is at least one Selected document with Status other than good or verygood 这种情况必须返回TRUE,如果阵列中至少一个Selected与文档Status比其他的goodverygood


Based on array from above. 基于上面的数组。 http://prnt.sc/de3gx9 You can see on the screenshot that in array is an object with: http://prnt.sc/de3gx9您可以在屏幕截图中看到数组中的对象是:

  1. isSelected isSelected
  2. Has other status than good or verygood 具有比其他状态goodverygood

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 如果在我的Array中至少有一个对象(isSelected = true,status = bad(或者除了good或verygood之外的任何其他)。那么我想看到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; 
    }

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: 你可以在没有lodash的情况下完成 - Array#someArray#some和一个谓词(谓词可以内联为匿名函数),如果谓词返回,结果将是真实的:

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); 

You could use lodash's flatMap to pluck the documents into one collection and then use some on the collection: 您可以使用lodash的flatMap将文档打包到一个集合中,然后在集合中使用一些

 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> 

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) 

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

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