2017-02-27 59 views
0

因此,我正在使用REST沙箱API從狀態數組中刪除「調用」,並且該數組包含所有JSON不知道如何使用它來刪除它。如何從數組中刪除json元素?

請注意:這是通過來自Lua Script ran Request的HTTP連接。

網站:getsandbox.com

下面的代碼:

//Store Calls 
 
state.calls = state.calls || [] 
 
var calls = state.calls; 
 

 

 

 
/* 
 
| 
 
| Create Call 
 
V 
 
*/ 
 

 
Sandbox.define('/new/call/','POST', function(req, res){ 
 
    
 
    var gameid = req.get("Roblox-Id"); 
 
    var person = req.body.split(" ")[0]; 
 
    var reason = req.body.split(" ")[1]; 
 
    var comp = { 
 
     gameid: gameid, 
 
     player: person, 
 
     callreason: reason 
 
    }; 
 
    
 
    calls.push(comp); 
 
    // Set the type of response, sets the content type. 
 
    res.type('text/plain'); 
 
    
 
    // Set the status code of the response. 
 
    res.status(200); 
 
    
 
    // Send the response body. 
 
    res.send('Request Accepted'); 
 
}); 
 

 
/* 
 
| 
 
| Get Calls 
 
V 
 
*/ 
 

 
Sandbox.define('/get/calls/','GET', function(req, res){ 
 
    // Check the request, make sure it is a compatible type 
 
    
 
    // Set the type of response, sets the content type. 
 
    res.type('application/json'); 
 
    
 
    // Set the status code of the response. 
 
    res.status(200); 
 
    
 
    // Send the response body. 
 
    res.send(calls); 
 
}) 
 

 
/* 
 
| 
 
| Delete 
 
v 
 
*/ 
 

 
Sandbox.define('/data/delete/{gameid}/','GET', function(req, res){ 
 
    for(i=0;i<calls.length;i++){ 
 
     calls[i].pop(); 
 
    } 
 
    
 
    // Set the type of response, sets the content type. 
 
    res.type('text/plain'); 
 
    
 
    // Set the status code of the response. 
 
    res.status(200); 
 
    
 
    // Send the response body. 
 
    res.send('Successful'); 
 
})

+0

我想你可能意味着'c alls.pop();',雖然「它不會讓我」並不真正告訴我哪部分代碼不工作。 – 4castle

+0

你如何識別需要刪除/刪除的元素?子密鑰的值?你提供的代碼中的哪一部分是你試圖刪除元素的?你有什麼嘗試? – haxxxton

回答

2

找到callindex和使用以下命令:

calls.splice(index, 1); 
+0

我如何indexOf它,如果它包含JSON – Dubst3p

+0

@ Dubst3p你可以使用'JSON.parse(jsonData)'將json文本轉換爲javascript對象並通過它進行搜索。 – Amir

+0

@ Dubst3p,它工作嗎? – Amir