这是我的数组(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"
}
]
}
]
我需要使用_.lodash写条件。
如果数组中至少有一个Selected
除Status
或good
之外的verygood
或good
基于上面的数组。 http://prnt.sc/de3gx9 您可以在屏幕截图中看到数组中的对象是:
verygood
或 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;
}
如果在我的数组中至少有一个对象(isSelected = true,状态=坏(或者除了好或非常好)。 然后我想看结果: TRUE
{{1}}
答案 0 :(得分:2)
你可以在Array#some
内没有lodash - Array#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);

答案 1 :(得分:1)
您可以使用lodash的flatMap将文档整理到一个集合中,然后在集合中使用some:
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>
&#13;
答案 2 :(得分:0)
您可以循环遍历它们并创建自己的自定义数组。
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)