2016-01-09 26 views
2

這是我的JSON數組。使用React.js更新JSON數組

rows : [ 
    { 
    "id":1, 
    "first_name":"William", 
    "last_name":"Elliott", 
    "email":"[email protected]", 
    "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。

我該怎麼做?

回答

2

通過你的數據,例如:

var data = { 
    rows: [ { 
      'id': 1, 
      'first_name': 'William', 
      'last_name': 'Elliott', 
      'email': '[email protected]', 
      '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