Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 564
rudeTool Avatar asked Mar 10 '26 10:03

rudeTool


2 Answers

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);
like image 110
Nitheesh Avatar answered Mar 12 '26 00:03

Nitheesh


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);
like image 20
Drew Reese Avatar answered Mar 12 '26 00:03

Drew Reese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!