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

259
Vistas
for each object array, how to add a field into it?

What I want is to insert a field index into my array

My code thar I receive my arrays is like that:

  const onFinish = (values) => {
       values.fields.map((el, idx) => {     
  console.log({ ...el, index: idx });
  //returns each object correctly, but how can I join these objects
     });

     addFields({ formName: 'TABLE NAME', fields: values.fields }).then(() => {
       //API CALL WHICH RETURNS A RESPONSE
     });
   };

values.fields is the array that I want to make it work

values.field array :

[
    {
        "field": "Peso do bezerro (kg)",
        "fieldtype": "Numeric"
    },
    {
        "field": "test",
        "fieldtype": "Text"
    },
    {
        "field": "Obs",
        "fieldtype": "Text"
    }
]

This array can have 'x' objects,What I want is for each add a field named index(which will get if it's the first and add 0 if the second will add a 1 and following...).Someone knows how to make it work ?

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

0

You can simply iterate for each item and add the index as shown below:

const onFinish = (values) => {
     console.log(values.fields);
     const fields = values.fields.map((item, index) => {...item, index});
     addFields({ formName: 'TABLE NAME', fields }).then(() => {
       //API CALL WHICH RETURNS A RESPONSE
     });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a simple function:

const yourArray = [{
    "field": "Peso do bezerro (kg)",
    "fieldtype": "Numeric"
  },
  {
    "field": "test",
    "fieldtype": "Text"
  },
  {
    "field": "Obs",
    "fieldtype": "Text"
  }
]

yourArray.forEach((item, i) => {
  item.index = i + 1;
});

console.log(yourArray);

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