gpt4 book ai didi

javascript - 使用 React.js 更新 JSON 数组

转载 作者:行者123 更新时间:2023-11-29 21:36:38 25 4
gpt4 key购买 nike

这是我的 JSON 数组。

 rows : [
{
"id":1,
"first_name":"William",
"last_name":"Elliott",
"email":"welliott0@wisc.edu",
"country":"Argentina",
"ip_address":"247.180.226.89",
"notificationType": [
{
"type": "update",
"text": "New Update"
"selected":null
},
{
"type": "user",
"text": "New User"
"selected":null
}
]
}
]

我需要更新这个数组。例如,让我们处理 notificationType 数据。

我想将“selected”:null 值更新为“selected”:true。

我该怎么做?

最佳答案

只需循环遍历您的数据,例如:

var data = {
rows: [ {
'id': 1,
'first_name': 'William',
'last_name': 'Elliott',
'email': 'welliott0@wisc.edu',
'country': 'Argentina',
'ip_address': '247.180.226.89',
'notificationType': [
{ 'type': 'update', 'text': 'New Update', 'selected': null },
{ 'type': 'user', 'text': 'New User', 'selected': null }
]
}
]
};

data.rows.forEach(function(row) {
row.notificationType.forEach(function(notif) {
notif.selected = true;
});
});

或者如果你想更新特定的通知:

data.rows[0].notificationType[1].selected = true;
//--------^ row index
//----------------------------^ notification index

关于javascript - 使用 React.js 更新 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34693120/

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