Almog Koren Almog Koren - 8 months ago 102
React JSX Question

Redux remove item from state based on object value

I have the following state in my redux store, it's photos which has an array of objects
enter image description here

Each object is different, I need to be able to find the object with the filename and remove the entire array item I have tried with index and the following but not really working

case actionTypes.PHOTO_DELETE:
// return state.filter((photo) => photo.filename !== action.data)

return { photos: state.photos.filter(photo =>
photo.filename !== action.data
)}

// let index = state.photos.findIndex((photo) => photo.fillname === action.data);
//console.log(index)
/*
var index = _.findIndex(action.data, function(photos) {
return photos.photos == action.data
})
*/
//return update(state, {photoGroups: {$splice: [[index]]}});

Answer Source

Filter the photos array by checking if the object with specified filename does not exist in the inner array. If it doesn't then findIndex() === -1.

return {
  photos: state.photos.filter(photo =>
    photo.findIndex(object => object.filename === action.data) === -1
  )
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download