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

104
Vistas
pushing object to array in reactjs?

i have a problem with pushing object to array.. i want a result like this.. [{b:"a"},{b:"c"}]

with this code

const [list, setList] - useState({occupation_id: ""});

const handleAdd = () => {
    let arr = [];
    const newData = {...list};
    newData[e.target.id] = e.target.value;
    arr.push(newData);
    setList(arr);
}

but the result from the code below is

[{{occupation_id: "2"}, occupation_id: "1"}]

what the problem with my code?

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

0

You can solve it easily with this solution, Try this:

const [list, setList] = useState([]); // your initial data
const handleAdd = () => {
 setList([...list, {e.target.id:e.target.value}]);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this :

const [list, setList] = useState([]);
const handleAdd = () => {
 setList([...list, {[e.target.id]:e.target.value}]);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

const [list, setList] = useState([{occupation_id: "1"}, {occupation_id: "2"}]);

  const handleAdd = (id, value) => {
    const newData = [...list]  
    newData.push({id, value});
    setList(newData);
    console.log(newData)
}
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