Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
Sorting the array of object in reducer state

I want to update the react state using redux but data is not sorting correctly

Original Array

    "sections": [
        {
            "id": 8,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "+96******",
            "type": "phone",
            "url": "tel:",
            "icon": "phone"
        }
     {
            "id": 9,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "test@gmail.com",
            "type": "email",
            "url": "",
            "icon": "email"
        }
    ]

I am updating the state using this code.

state = { ...state,sections :[ ...state.sections.filter(
                (section) => section.id !== action.payload.section.id
              ) , action.payload.section ]  }
            return state

After updating the array objects are getting reversed

   "sections": [
       {
            "id": 9,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "test@gmail.com",
            "type": "email",
            "url": "",
            "icon": "email"
        }
        {
            "id": 8,
            "user_id": 1,
            "field_type_id": 8,
            "section_id": 3,
            "value": "+91344******",
            "type": "phone",
            "url": "tel:",
            "icon": "phone"
        }
    ]

How can i stop the array reversing?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you are simply wanting to update an element at a specific index then just use Array.prototype.map to map the previous array to the next, updating the specific element when it's reached. Array order is maintained.

const nextState = {
  ...state,
  sections: state.sections.map(
    section => section.id === action.payload.section.id
      ? action.payload.section
      : section
  ),
};
return nextState;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use one of 2 :

 // filter
  const state = {
    ...state,
    sections: state.sections.filter(
      (section) => section.id !== action.payload.section.id
    ),
  };
  
  // Update
  const state = {
    ...state,
    sections: state.sections.map((section) =>
      section.id !== action.payload.section.id
        ? { ...section, url: 'new Value' }
        : section
    ),
  };

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda