0

I have a JSON data object which I am retrieving from mongoDB. It has the following format:

var billData = [{
    "_id": "PT155/454",
    "_class": "com.aventyn.hms.domain.OPDBill",
    "billingDate": "2017-11-20",
    "patientId": "PT155",
    "transactions": [{
        "txnId": "PT155/454/1",
        "toBePaid": "0",
        "txnAmount": "0",
        "due": "0",
        "selfPay": true
    }, {
        "txnId": "PT155/454/2",
        "toBePaid": "450",
        "txnAmount": "350",
        "due": "100",
        "selfPay": false
    }]
}, {
    "_id": "PT156/455",
    "_class": "com.aventyn.hms.domain.OPDBill",
    "billingDate": "2017-11-20",
    "patientId": "PT156",
    "transactions": [{
        "txnId": "PT156/455/1",
        "toBePaid": "300",
        "txnAmount": "200",
        "due": "100",
        "selfPay": true
    }, {
        "txnId": "PT156/455/2",
        "toBePaid": "100",
        "txnAmount": "50",
        "due": "50",
        "selfPay": true
    }]
}];

My problem is I want to remove those transactions which have property of selfPay: false for this I am doing the following but It is not working:

$.each(billData, function (k, v) {
    $.each(v.transactions, function (tK, tV) {
        if (tV.selfPay == true) {
            billData[k].transactions = tV;
        }
    });
});

But the data which I get is same as what I am getting from the database.

Any idea how can I achieve this?

Thanks for your Help.

Link for jsfiddle is: https://jsfiddle.net/0uy38pLf/

2
  • v.transaction should be v.transactions, no? Commented Nov 20, 2017 at 8:09
  • yes it is v.transactions, it was just a typing mistake while typing for this question. Commented Nov 20, 2017 at 8:12

2 Answers 2

3

You can use a combination of Array#map and Array#filter to achieve the desired results (without even using any method of jQuery):

var billData = [{
    "_id": "PT155/454",
    "_class": "com.aventyn.hms.domain.OPDBill",
    "billingDate": "2017-11-20",
    "patientId": "PT155",
    "transactions": [{
        "txnId": "PT155/454/1",
        "toBePaid": "0",
        "txnAmount": "0",
        "due": "0",
        "selfPay": true
      },
      {
        "txnId": "PT155/454/2",
        "toBePaid": "450",
        "txnAmount": "350",
        "due": "100",
        "selfPay": false
      }
    ]

  },
  {
    "_id": "PT156/455",
    "_class": "com.aventyn.hms.domain.OPDBill",
    "billingDate": "2017-11-20",
    "patientId": "PT156",
    "transactions": [{
        "txnId": "PT156/455/1",
        "toBePaid": "300",
        "txnAmount": "200",
        "due": "100",
        "selfPay": true
      },
      {
        "txnId": "PT156/455/2",
        "toBePaid": "100",
        "txnAmount": "50",
        "due": "50",
        "selfPay": true
      }

    ]
  }
];

var result = billData.map(bill => {
  bill.transactions = bill.transactions.filter(tran => tran.selfPay);
  return bill;
});

console.log(result);

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

2 Comments

thank you this one helps a lot.. can you tell if I have to check 2 condition then how to write like in if we use && or || operator to check different condition, can we do this here also
@VipulSingh -- yes you can. .filter()'s callback should return a boolean expression to include item in the result.
1

As far as I can see, it could be better, you negate the if clause. So that you check against selfPay === false.

console.log(billData);
$.each(billData,function(k,v){
    $.each(v.transactions,function(tK,tV){
        if(tV.selfPay === false ){
            billData[k].transactions.splice(tK, 1);
        }
    });
});
console.log(billData)

Also I guess you want to remove just that single item. To remove a single item use the splice function which takes the index of the item you want to remove in this case tK.

Hope it helps!

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.