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

102
Vistas
add new field in every json object in a array javascript

I am working on a react app where I am fetching a JSON response from a route which is basically a list of JSON objects.

Now I want to add a field in every JSON object according to another field in that JSON object:

Here's my response:

{
  messages: [
    {
      msgId: "2021082111010755885",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-21 11:01:07.89554",
      modifiedAt: "2021-08-21 11:01:07.89554",
      count: 0,
    },
    {
      msgId: "2021082012204615964",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:31:06.622",
      count: 5,
    },
    {
      msgId: "2021082012204575430",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:27:06.473",
      count: 3,
    },
    {
      msgId: "2021082012204613152",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:28:06.517",
      count: 3,
    },
  ];
}

Now I want to insert a field named mString which is status+"A" where status is from the same object.I want to insert it in every object in the above array.How can I do this?

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

0

Just loop though array and add

const data = {
  "messages": [
    {
      "msgId": "2021082111010755885",
      "interfaceId": 5,
      "status": "QUEUED",
      "createdAt": "2021-08-21 11:01:07.89554",
      "modifiedAt": "2021-08-21 11:01:07.89554",
      "count": 0,

    },
    {
      "msgId": "2021082012204615964",
      "interfaceId": 5,
      "status": "QUEUED",
      "createdAt": "2021-08-20 12:20:46.187297",
      "modifiedAt": "2021-08-20 12:31:06.622",
      "count": 5,

    },
    {
      "msgId": "2021082012204575430",
      "interfaceId": 5,
      "status": "QUEUED",
      "createdAt": "2021-08-20 12:20:46.187297",
      "modifiedAt": "2021-08-20 12:27:06.473",
      "count": 3,

    },
    {
      "msgId": "2021082012204613152",
      "interfaceId": 5,
      "status": "QUEUED",
      "createdAt": "2021-08-20 12:20:46.187297",
      "modifiedAt": "2021-08-20 12:28:06.517",
      "count": 3,

    }
  ]
}

data.messages.forEach((node) => node.mString = node.status + "A");

console.log(data);

about 4 years ago · Juan Pablo Isaza Denunciar

0

In React I would suggest generally using an immutable update pattern where you return a new array with the element object properties you want. This uses Array.prototype.map.

const newData = {
  ...data,
  messages: data.messages.map(el => ({
    ...el,
    mString: el.status + "A",
  })),
};

const data = {
  messages: [
    {
      msgId: "2021082111010755885",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-21 11:01:07.89554",
      modifiedAt: "2021-08-21 11:01:07.89554",
      count: 0,
    },
    {
      msgId: "2021082012204615964",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:31:06.622",
      count: 5,
    },
    {
      msgId: "2021082012204575430",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:27:06.473",
      count: 3,
    },
    {
      msgId: "2021082012204613152",
      interfaceId: 5,
      status: "QUEUED",
      createdAt: "2021-08-20 12:20:46.187297",
      modifiedAt: "2021-08-20 12:28:06.517",
      count: 3,
    },
  ],
};

const newData = {
  ...data,
  messages: data.messages.map(el => ({
    ...el,
    mString: el.status + "A",
  })),
};

console.log(newData);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I did not understand you very well in the last part but I think this is what you want

 object.messages.map(element=> {
   element.mystring=el.status + "A"
    return  element
}))

your need to access to "messages" in your JSON with dot notation

object.messages is an array you can map the objects inside (map)

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