简体   繁体   中英

How to splice the value from arrays in javascript/jquery?

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:

在此处输入图片说明

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?

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM