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
Source: stackoverflow.com
Related Query
- make existing object in array the last item in the array REDUX
- Is there a way to skip the yup validation to the last object in an array of objects?
- How add item in nested array of object in redux reducer
- Update a redux array of object but not re-render the component
- onClick event only leaves the last item of the DOM array instead of removing the target
- Scroll to the last item of a mapped array
- Removing an item from an array of items only removes the last item
- ClassName only takes the last item from my array
- Use SetState to update one of the item in array object in react
- Iterate over array of object using map till the second last object
- React Hooks Form Handling: Update the state of an object with multiple string items and one array item
- Array.push is pushing only last object in the array
- Redux toolkit combineReducers problem.:Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose
- React - map function sets the input values only with the last item in the array
- React List and Keys - How to make last added item appearing at the top?
- Redux reducer: Add object to array when the array doesn't have the object data
- How to update an existing array of objects (add a new object to the array) with the useState hook?
- Redux - returning an array instead of an object in the reducer
- React redux - pushing new item to the state array not working
- How can I change the value of an object in an array in react redux when the action is called?
- Merging values from an item in an array and an object in redux
- Mapping to React Modal only renders the last item of the array
- Removing specific item from nested array and saving the result, how to make this code cleaner
- Redux not updating state correctly -- state stays as empty array despite adding an object in the line prior
- How to add a new item into array in an object that's is in the array?
- Map function stores the value of the last object in the array to all values
- Add item to the beginning of sorted object array
- Error React Redux Toolkit: Why I can't change the key value the array object using .findIndex
- Moving first item in array to the last position
- Redux add item to an existing object in state duplicates entry
More Query from same tag
- ReactJs - How can I add an 'active' class to the nav bar buttons depending on which page is currently being used?
- Two imports div in map()
- React. Cannot get the component element in the DOM
- How to pass import as props parameters in React JS
- ReactTestUtils has been moved
- How do i get which element is clicked/focused in blur event of another element
- creating "gated" react components
- React - child component not updating when receiving new props
- How do I use search in react?
- React.PropTypes is deprecated since React 15.5.0
- How to get label values dynamically and adding the numbers together from different input with reactjs
- How to import ReactCSSTransitionGroup using es6 syntax?
- Passing props to dynamically loaded components
- query string react-router path
- i want to change the selected values and also update with using select and option with using react js
- How to pass in the current date React JS Mongo
- React SSR: Document is not defined
- Jest coverage - if statement in switch statement
- Picking up a prop when passed from grandparent to parent component
- ReactJS onClick does not work for the button, the function inside onClick is not called
- React - call function in child component if parent state changes
- <Navigate/> react-router-dom v6
- I would like to use TypeScript and React in Laravel Project
- Remove element from array using react
- cleaner way to check whether or not a the current window.location.href is equal to any of the strings in the array
- Spring JPA One2Many Relationship ids are not updated/inserted correctly
- is there any solution for my react state problem?
- Socket.io in deeply nested Express Router
- In Svelte, is there a way to undo invalid user input, without changing the state?
- TypeError: Cannot read property 'find' of undefined in React-redux while running a test