javascript - 如何从数组中删除 json 元素?

标签 javascript arrays json rest http

因此,我使用 REST 沙箱 API 从状态数组中删除“Call”,并且该数组包含所有 JSON,不确定使用什么来删除它。

请注意:这是通过 HTTP 从 Lua 脚本运行的请求进行连接。

网站: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');
})

最佳答案

找到调用索引并使用以下内容:

calls.splice(index, 1);

关于javascript - 如何从数组中删除 json 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476388/

相关文章:

javascript - 打印时将元素设置为内联显示,该元素设置为无

javascript - 如何使用 jqplot 绘制自定义水平线?

javascript - 使用 JavaScript 查看变量是否为数组

arrays - 想从tableview中删除一行,并从数组中删除对应的对象

javascript - JavaScript "class"的规范术语

python - 如何在Holoviews中使用HoloMap查看2D数组切片(Python 3.5.1)

javascript - 在javascript中修改具有已知位置的JSON对象的值

json - 从 stats.nba.com 发布抓取数据

php - 如何在php中获取标准输出json

javascript - 整理 Javascript,捕获和操作 URL