Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete Multiple array items

I'm using React js. I want to add an option to delete multiple items. but after deleting each item, page refreshes the props and not delete remaining items.

How can I delete Multiple items?

const onDeleteAll = arr => {
        
    arr.forEach(element => {
      const formData = {
        id:element
      }

      props.onDeleteSubmit(formData, function(){ // pass id to delete func
        console.log('deleted')
      })
    });
  }
  useEffect(() => {
    props.getPriceType(); // fetching data
  }, []);

reducer:

case DELETE_PRICE_TYPE_SUCCESS_ACTION:
          const myDeletedArray = draft.list;
          const objDeletedIndex = myDeletedArray.filter(obj => obj.id !== action.payload._id);
          draft.list = objDeletedIndex; //update data
          break;
like image 787
kinza Avatar asked May 24 '26 16:05

kinza


1 Answers

var id = 23;
var list = [{
  id: 23,
  value: "JOHN"
}, {
  id: 23,
  value: "JADE"
}, {
  id: 24,
  value: "JADE"
}, {
  id: 25,
  value: "JAMES"
}];

var indexes = [];
const templist = list.filter((item, ind) => {
  return item.id !== id
});
list = templist;
console.log(list);

First get the list of indexes which matches the items in the array to be deleted. Traverse the above list and delete the items from each array by splice operator.

like image 65
Asutosh Avatar answered May 27 '26 06:05

Asutosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!