Estoy tratando de implementar una funcionalidad en la que quiero insertar un objeto en una matriz cuando el usuario lo marcó y eliminarlo si el usuario no lo selecciona. Tengo un menú donde estoy recopilando opciones de usuario. He implementado el código pero no ha resuelto mi problema. ¿Podría alguien ayudarme a resolver este problema? Gracias
const selectSingle = (id, item, index) => { const user = Cookies.get("user") && JSON.parse(Cookies.get("user")); let callScheduleIds = Object.assign([], allCallNotifications); if (callScheduleIds.findIndex((item) => item.order_id === id)) { callScheduleIds.push({ order_id: id, phone_number: item.phone1, sender_id: user.user.id, }); } else { callScheduleIds.splice(callScheduleIds.indexOf(id), 1); } setAllCallNotifications(callScheduleIds); };Puedes hacerlo usando Lodash .
const selectSingle = (rowIndex, item) => { let callIds = Object.assign([], allCallNotifications) if (_.findIndex(callIds, { id: item.id }) === -1) { callIds.push(item) } else { callIds.splice(_.findIndex(callIds, { id: item.id }), 1) } setAllCallNotifications(callIds) }