Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda