2016-08-02 25 views
0

如何用一個新的更新對象用NodeJS替換舊的json對象?如何用NodeJS替換一個新的json對象

現在,當我更新json文件時,它將保存舊數據的新數據。

JSON:

[ { 
    "id": 1, 
    "name": "Sven", 
    "phone": "123123" 
    }, 
    { 
    "id": 2, 
    "name": "Martin", 
    "phone": "2342342" 
    } ] 

這裏是我的代碼:

var operation = POST.operation; // POST request comes with operation = update/insert/delete 


     if (operation == 'update') { 
      fs.readFile("file.json", "utf8", function (err, data) { 

       var jsonFileArr = []; 
       jsonFileArr = JSON.parse(data); //Parse the data from JSON file      
       var haveId = jsonFileArr.some(function (obj){ // Checks if the POST request have the same id as JSON file 
        return obj.id == POST.id; 
       }) 

       if (haveId) { // if true 

        var updateData = []; // Array with POST data 
        updateData.push({ 
         id: POST.id, 
         name: POST.name, 
         phone: POST.phone, 
        }) 
        jsonFileArr.push(updateData); 
        var newUsers = JSON.stringify(jsonFileArr); 
        fs.writeFile("file.json", newUsers, "utf8"); 
        console.log(err); 
       } 
      })     
     } 

我也許應該使用delete object但我怎麼可以指定應刪除哪些對象?

因此,當我更新ID爲1的數據,它會刪除舊的ID /名稱/電話,並寫入新的數據。

回答

2

我的假設基於你的問題是你在一個文件中有多個對象。因此,解決這個問題的簡單方法是

 if (operation == 'update') { 
     fs.readFile("file.json", "utf8", function (err, data) { 

      var jsonFileArr = []; 
      jsonFileArr = JSON.parse(data); //Parse the data from JSON file 
      var haveId = jsonFileArr.some(function (obj){ // Checks if the POST request have the same id as JSON file 
       return obj.id == POST.id; 
      }) 

      if (haveId) { // if true 
       var updateData = []; // Array with POST data 
       updateData.push({ 
        id: POST.id, 
        name: POST.name, 
        phone: POST.phone, 
       }) 

       for(let Arr of jsonFileArr){ 
        if (Arr.id == POST.id){ 
        let currentIndex = jsonFileArr.indexOf(Arr); 
        jsonFileArr.splice(currentIndex,1,updateData) //removing the old object and adding the new one 
        } 
       } 

       var newUsers = JSON.stringify(jsonFileArr); 
       fs.writeFile("file.json", '', "utf8",function(err,res){ //Making the file empty 
        if(!err){ 
        fs.writeFile("file.json", newUsers, "utf8",function(err,res){ //Writing the whole object back 
         if(err)console.log(err); 
         console.info(res); 
        }); 
        }else{ 
        console.log(err); 
        } 
       }); 

      } 
     }) 
    } 

我認爲這是更好的,而不是使用一些,得到匹配的索引,並直接替換。

 var jsonFileArr = JSON.parse(data); //Parse the data from JSON file 
     var foundId = jsonFileArr.findIndex(function (obj){ // Checks if the POST request have the same id as JSON file 
      return obj.id == POST.id; 
     }); 
     if (foundId >= 0) { 
     jsonFileArr[foundId] = 
      { 
       id: POST.id, 
       name: POST.name, 
       phone: POST.phone, 
      } 
     } 

....然後寫回文件

+0

哇感謝,它的工作原理!但是你能解釋'for(var Arr of jsonFileArr)'Loop。 (取代讓變量,因爲我的javascipt不支持讓) 我很喜歡新的。 – Bongskie

+1

[for ...](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)遍歷一個可迭代的對象,在這種情況下是一個數組。它爲數組中的每個項目運行循環的主體,爲當前項目分配名稱「Arr」。節點6+應該支持'let'; 4和5具有[部分支持](http://kangax.github.io/compat-table/es6/#test-let),儘管它們沒有適當的塊範圍。 –

+0

@UselessCode,葉,當我使用'let'時,我得到一個塊範圍的聲明錯誤。我認爲是時候升級:)謝謝你的解釋 – Bongskie

相關問題