score:0

yes my code was almost correct. i needed to add the brackets to unsubgroup to make sure and return the object instead of an array with an. object inside.

i was getting a result like this where the unsubgroup was an array with an object, i only wanted the object.

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;
    }
}

above function gave me below output ( needed to add the [0] to end of unsubgroup filter method

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

score:1

your code is correct, but .push returns the number of elements in the result, instead of the actual result, all you have to do is move the console.log one line down:

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)

however, i think you should return allothergroups instead of return action.payload


Related Query

More Query from same tag