0

I have the below Array of object set, I need to take the some of them based on the condition from the whole array set using lodash.

I have used _.filter(data, condition) in lodash, but expected result is not coming.

Anyone help me to resolve this ?

Condition : EarnCode < 90 && Hours === 0 (result will be skipped those data).

Request Array set:

[
  {
    "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
  }
]

Expected Result:

[
{
        "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
      }
]
5
  • 3
    Show us the code you tried out. Commented Feb 28, 2018 at 17:36
  • Unrelated, why do you want to add a library like lodash for doing this? Commented Feb 28, 2018 at 17:38
  • 2
    just reverse your condition and you will get what you want Commented Feb 28, 2018 at 17:38
  • Yeah, just reverse the condition (^^) to EarnCode >= 90 || Hours !== 0 Commented Feb 28, 2018 at 17:58
  • Use lodash's reject instead of filter with your condition Commented Feb 28, 2018 at 20:29

2 Answers 2

3

Just for completeness, following CRice's answer which is likely the way to go about this, if you insist that you must use lodash:

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>

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

Comments

2

No need to use lodash (if you don't want to), since the built in array .filter method will be enough to do this.

All you have to do is give it the right condition. In your case, that the item does not have 0 hours and an EarnCode less than 90. That condition can be represented as either:

!(EarnCode < 90 && Hours === 0)

or eqivalently:

EarnCode >= 90 || Hours !== 0

Using that second one, your filter might look like this:

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.

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.