使用React.js更新JSON数组

Update JSON Array with React.js

本文关键字:JSON 数组 更新 js React 使用      更新时间:2023-09-26

这是我的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