0

I have declared an array as var user_profile = []; and I have already some pushed data in arrayList which contains :

[  
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"appeal",
      "assessAmount":"445",
      "assessPenalty":"",
      "assessTotal":445
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"appeal",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"445",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"2075-09-13",
      "assessAmount":"2075-09-03",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"2075-09-13",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":0,
      "assessReason":"2075-09-03",
      "assessTotal":null
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"40",
      "assessTotal":43
   },
   {  
      "assessmentType":"PRE",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

In those list of arrays i need to remove the data whose assessCatId==0. So I tried to splice using

$.each(user_profile, function(i) {
  if (user_profile[i].assessCatId == 0) {
    user_profile.splice(i, 1);
  }
});

But it is not working.It is showing me error as:

enter image description here

Or I mean to say I just need the data in arrayList whose assessCatId==1 or 2 or 3. How to filter this arrayList and remove those data from arrayList?

1
  • hey you should take the index and splice it out Commented Dec 27, 2018 at 5:12

1 Answer 1

2

Don't try to mutate (eg splice) an array while you're iterating over it - it will result in very unintuitive behavior. For example, after the first element gets removed, further iterations' i will refer the index that the element being iterated over used to be at, not the index that the element is at in the new mutated array - resulting in undefined problems near the end, and in incorrect indicies being removed.

A simple .filter would be a better option here, no need for jQuery:

const input=[{"assessmentType":"PRE","assessCatId":0,"assessReason":"appeal","assessAmount":"445","assessPenalty":"","assessTotal":445},{"assessmentType":"PRE","assessCatId":0,"assessReason":"appeal","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"445","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-13","assessAmount":"2075-09-03","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-13","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-03","assessTotal":null},{"assessmentType":"PRE","assessCatId":1,"assessReason":"A","assessAmount":"1","assessPenalty":"2","assessTotal":3},{"assessmentType":"PRE","assessCatId":2,"assessReason":"B","assessAmount":"3","assessPenalty":"40","assessTotal":43},{"assessmentType":"PRE","assessCatId":3,"assessReason":"C","assessAmount":"5","assessPenalty":"6","assessTotal":11}]

const output = input.filter(({ assessCatId }) => assessCatId != 0);
console.log(output);

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

5 Comments

filter requires significantly less code (less surface area for bugs) and is much more intuitive than splice for this situation - it's definitely preferable to the alternative.
@CertainPerformance what if i need to add condition to check both condition assesscatId=0 and assessReason== empty.
@ashwin Then extract both properties: ({ assessCatId, assessReason }) => assessCatId != 0 || assessReason != 'empty') or something of the sort
@CertainPerformance the way u said me to filter assessReason is not working .Please help me
It's not 100% clear what you're looking for, what sort of output are you expecting? Note that you currently do not have any assessReason properties which are 'empty' in the input array

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.