2

I have the following data :

removed_users = [1]

And

userProfile = [{id:1 , user:{id:1,username:test}} ,
               {id:2 , user:{id:2,username:test2}} ,]

What i wish to do: I wish to be able to remove the correct objects from userProfile , based on the array removed_users . I have tried the following code below but its not removing it from the array

state.project['userProfile'].filter(function(user) {
                                                    return !action.payload.find(function(removed) {
                                                            return removed === user.user.id
                                                                    })
                                                                })}

This is the code for the reducer thats supposed to help me remove the removed_users from the state

        case 'user_remove': return (updateObject(state, {
        project: {...state.project , ['users']:  state.project['userProfile'].filter(function(user) {
                                                                    return !action.payload.find(function(removed) {
                                                                    return removed === user.user.id
                                                                    })
                                                                })}
    }))

This is the script for updateObject helper function:

export const updateObject = (oldObject, updatedProperties) => {
return {
    ...oldObject,
    ...updatedProperties
}

}

5
  • It seems that you have a mistake in .filter return. Try to return action.payload.find instead of return !action.payload.find Commented Jul 13, 2020 at 7:53
  • I think you want to modify your state stored in state.project['userProfile'], but your reducer is storing result in state.project['users']. Could you send also your action creator function? I can only guess now what action.payload is? Commented Jul 13, 2020 at 8:02
  • code is working fine. the question is what does action.payload hold? Commented Jul 13, 2020 at 8:07
  • action.payload holds the removed_users i apologize! yes its because of the ! sign Commented Jul 13, 2020 at 8:10
  • the ! sign? but you said that you need to remove these entries, so filter will return all the other ones, based on ! sign. Commented Jul 13, 2020 at 8:11

3 Answers 3

1

You have a mistake in the filter. Use expression like this:

userProfile.filter((user)=>!removed_users.includes(user.id))

See full example in the playground: https://jscomplete.com/playground/s534466

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

Comments

0

Try this to remove user:

const userProfile = [
  {id:1 , user:{id:1,username:"t1"}} ,
               {id:2 , user:{id:2,username:"t2"}}]

const arr= [1];
const filtered = userProfile.filter(({user})=> !arr.includes(user.id)); 
console.log(filtered)

Comments

0

Steps:

  1. Dispatch an action for deleting a user by its id.
function removeUser(id){
   return {
     type: "REMOVE_USER",
     payload: id
   }
}
dispatch(removeUser(1))
  1. Filter users in the reducer that the users id is not equal with received id. And then set new filtered users in the state which not include removed users.
function reducer(state, action){
   switch(action.type){
     case "REMOVE_USER":
        return {
          ...state,
          users: state.users.filter(user => user.id !== action.payload),
        }
     // other cases
   }
}

Good luck.

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.