我有下面的对象数组,我需要根据整个数组中使用lodash的条件来获取其中一些。
我在lodash中使用了_.filter(data,condition),但预期的结果却没有 未来。
有人帮我解决这个问题吗?
条件:EarnCode< 90&&小时=== 0(结果将跳过这些数据)。
请求数据集:
[
{
"id" : 1,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 96,
"Hours" : 0
},
{
"id" : 3,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 96,
"Hours" : 0
},
{
"id" : 2,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 1,
"Hours" : 0
},
{
"id" : 5,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 1,
"Hours" : 3
}
]
预期结果:
[
{
"id" : 1,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 96,
"Hours" : 0
},
{
"id" : 3,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 96,
"Hours" : 0
},
{
"id" : 5,
"EmpId" : 100,
"AcctCode" : "2001-00",
"SuperID" : 2000,
"ReportNo" : "20180213.2015-06.2768.6",
"EarnCode" : 1,
"Hours" : 3
}
]
答案 0 :(得分:3)
为了完整起见,如果你坚持要使用lodash,可以按照CRice的回答来解决这个问题:
const inputSet = [{
"id": 1,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 96,
"Hours": 0
}, {
"id": 3,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 96,
"Hours": 0
}, {
"id": 2,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 1,
"Hours": 0
}, {
"id": 5,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 1,
"Hours": 3
}]
// Negate your condition, filter keeps items where the condition is
// true, so for your example, you need either Hours to be non-zero
// OR for EarnCode to be greater than 90.
const resultSet = _.filter(inputSet, i => i.Hours !== 0 || i.EarnCode >= 90);
console.log(resultSet); // Includes items 1, 3, and 5.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
答案 1 :(得分:2)
不需要使用lodash(如果你不想),因为内置的数组.filter
方法就足够了。
你所要做的就是给它正确的条件。在您的情况下,该项目没有0小时,EarnCode小于90.该条件可以表示为:
!(EarnCode < 90 && Hours === 0)
或等效地:
EarnCode >= 90 || Hours !== 0
使用第二个,您的过滤器可能如下所示:
const inputSet = [{
"id": 1,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 96,
"Hours": 0
}, {
"id": 3,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 96,
"Hours": 0
}, {
"id": 2,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 1,
"Hours": 0
}, {
"id": 5,
"EmpId": 100,
"AcctCode": "2001-00",
"SuperID": 2000,
"ReportNo": "20180213.2015-06.2768.6",
"EarnCode": 1,
"Hours": 3
}]
// Negate your condition, filter keeps items where the condition is
// true, so for your example, you need either Hours to be non-zero
// OR for EarnCode to be greater than (or equal to) 90.
const resultSet = inputSet.filter(i => i.Hours !== 0 || i.EarnCode >= 90);
console.log(resultSet); // Includes items 1, 3, and 5.