0

I'm working on react app with redux. I want to delete multiple item from array. I write below code in my reducer which delete single item from array but i want to delete multiple item.

case DELETE_LINK:  
    let dltLink = state.filter(item => {
            return item._id !== action.data._id

    }) 
    return {
        ...state,
        parentFolderlinks: dltLink
    };
1
  • ID's tend to indicate uniqueness (or they should!), so using any specific id likely won't allow you to remove multiple elements from an array. Can you provide more detail as to what needs to be deleted from a links array? Commented Jun 12, 2020 at 7:13

3 Answers 3

1

It seems you want to filter links from state.parentFolderlinks, say you have the ids in action.data.ids, you could

case DELETE_LINK:
    const parentFolderlinks = state.parentFolderlinks.filter(item => {
            return !action.data.ids.includes(item._id);
    });
    return {
        ...state,
        parentFolderlinks
    };
Sign up to request clarification or add additional context in comments.

1 Comment

return !action.data.ids.includes(item.id) would be more effective as it doesn't have to go through the whole action.data.ids array.
1

On what basis would you like to filter items? I assume that multiple items will not have the same id.

Below example shows how we can filter multiple items in redux. In this case, foods state with items that has type as fruit and removes everything else.

// initial state with all types of foods
const initialState = {
    "foods": [
        {
            name: "apple", 
            type: "fruit"
        }, 
        {
            name: "orange", 
            type: "fruit"
        }, 
        {
            name: "broccoli", 
            type: "vegetable"
        }, 
        {
            name: "spinach", 
            type: "vegetable"
        }, 
    ]
}

// sample reducer that shows how to delete multiple items 
export default (state = initialState, { type, payload }) => {
    switch (type) {
    
    // delete multiple items that does not have type fruit
    // i.e both brocolli and spinach are removed because they have type vegetable
    case DELETE_ITEMS_WITHOUT_TYPE_FRUIT:
        const onlyFruits = state.foods.filter(food => food.type === "fruit");

        return {
            ...state, 
            foods: onlyFruits
        }
    }
}

Comments

-1

you could map over the state and run it through a function that works out if you want to keep it or not (I don't know what your logic is for that) then return the array at the end

const keepThisItem =(item) => {
   return item.keep
}

case DELETE_LINK:
    let itemsToKeep = []  
    let dltLink = state.map(item => {
        if(keepThisItem(item){
            itemsToKeep.push(item)
        }
        return itemsToKeep
    }) 

1 Comment

For iteratring over an array the forEach method I (without a return) is what you want. Using map here would return an array of arrays to keep. Not sure this answers the op question, either

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.