Estoy obteniendo algunos datos de Mongodb. Los datos son una matriz de objetos. Me gustaría hacer que el objeto con group.title === "darse de baja" sea siempre el último objeto de la matriz. la matriz siempre tendrá una longitud variable.
Estoy usando React con Redux y trato de empujar mi objeto con el título "darse de baja" al final de la matriz para que siempre pueda representarlo como el último elemento en una tabla
Aquí está mi reductor. el console.log me muestra 3 no la matriz de objetos
import { LIST_GROUPS, LIST_SHOW_GROUPS } from '../actions/types'; export default function (state = [], action) { switch (action.type) { // when we logout, this action.payload is an empty string so lets do || false case LIST_GROUPS: const unSubGroup = action.payload.filter((group) => group.title === "unsubscribers") const allOtherGroups = action.payload.filter((group) => group.title !== "unsubscribers")[0] console.log(allOtherGroups.push(unSubGroup)) return action.payload case LIST_SHOW_GROUPS: return action.payload.filter((el) => { return !el.hide }) default: return state; } }Su código es correcto, pero .push devuelve la cantidad de elementos en el resultado, en lugar del resultado real, todo lo que tiene que hacer es mover el console.log una línea hacia abajo:
const unSubGroup = action.payload.filter((group) => group.title === "unsubscribers") const allOtherGroups = action.payload.filter((group) => group.title !== "unsubscribers")[0] allOtherGroups.push(unSubGroup) console.log(allOtherGroups) Sin embargo, creo que debería return allOtherGroups en lugar de return action.payload
Sí, mi código era CASI CORRECTO. Necesitaba agregar los corchetes a unSubGroup para asegurarme y devolver el objeto en lugar de una matriz con an. objeto dentro.
Obtuve un resultado como este en el que unSubGroup era una matriz con un objeto, solo quería el objeto.
0: {contacts: Array(445), _id: '6266fa8df11e', title: 'presidential', created: '2022-04-25T19:46:21.792Z', hide: false} 1: {contacts: Array(0), _id: '626a0d61db63', title: 'rolex', hide: false, created: '2022-04-28T03:43:29.664Z'} 2: [{…}] import { LIST_GROUPS, LIST_SHOW_GROUPS } from '../actions/types'; export default function (state = [], action) { switch (action.type) { // when we logout, this action.payload is an empty string so lets do || false case LIST_GROUPS: const unSubGroup = action.payload.filter((group) => group.title === "unsubscribers")[0] const allOtherGroups = action.payload.filter((group) => group.title !== "unsubscribers") allOtherGroups.push(unSubGroup) console.log("allOtherGroup", allOtherGroups) return action.payload case LIST_SHOW_GROUPS: return action.payload.filter((el) => { return !el.hide }) default: return state; } }la función anterior me dio la siguiente salida (necesitaba agregar el [0] al final del método de filtro unSubGroup
0: {contacts: Array(445), _id: '6266fa8df', title: 'presidential', created: '2022-04-25T19:46:21.792Z', hide: false} 1: {contacts: Array(0), _id: '626a0d61db', title: 'rolex', hide: false, created: '2022-04-28T03:43:29.664Z'} 2: {contacts: Array(0), _id: '626a0bc2', title: 'unsubscribers', hide: false, created: '2