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

125
Vistas
adding item in to react state at certain position related to the item at hand?

How to update state array to add a value after related position rather than at the end of the array ? in the example below for example I want rice to go after rice rather than after bananas ? , this current logic is making thing really mixed when I want to render items according to what the user has selected to be the first item.

let stateArr = [{item : cucumber}, {item : rice }, {item: bannan }, {item : rice }]

// current state logic 

[...stateArr, newItem ]

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

0

If you want to keep things immutable, you can slice your array at the point you want to insert the new item, and spread those two slices around your new value.

let stateArr = [
    { item: "cucumber" },
    { item: "rice" },
    { item: "bannan" },
    { item: "fish" },
];

// First, find the location of the item you want to insert _after_
const riceIndex = stateArr.findIndex(({ item }) => item === "rice");

const newItem = { item: "crab" };

// Next, slice _right after_ that item (+ 1)
const newState = [
    ...stateArr.slice(0, riceIndex + 1),
    newItem,
    ...stateArr.slice(riceIndex + 1),
];

console.log(newState);

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