gpt4 book ai didi

javascript - 在数组javascript中的每个json对象中添加新字段

转载 作者:行者123 更新时间:2023-12-05 00:52:53 25 4
gpt4 key购买 nike

我正在开发一个 react 应用程序,我正在从基本上是 JSON 对象列表的路由中获取 JSON 响应。

现在我想根据 JSON 对象中的另一个字段在每个 JSON 对象中添加一个字段:

这是我的回复:

{
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,
},
];
}

现在我想插入一个名为 mString 的字段,它是 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,

}
]
}

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

console.log(data);

关于javascript - 在数组javascript中的每个json对象中添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69041146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com