Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取数组中的最后x元素无效_Javascript_Node.js_Express - Fatal编程技术网

Javascript 获取数组中的最后x元素无效

Javascript 获取数组中的最后x元素无效,javascript,node.js,express,Javascript,Node.js,Express,尝试使用下面的答案: 我正在使用express/node 我有 var texts = [{"id":1, "content":"one"}, {"id":2, "content":"two"},{"id":3, "content":"three", "id":4, "content":"four"]; 在get请求中,我尝试如下响应: app.get('/api/texts/', function (req, res, next) { res.json(texts.slice(3,

尝试使用下面的答案:

我正在使用express/node

我有

var texts = [{"id":1, "content":"one"}, {"id":2, "content":"two"},{"id":3, "content":"three", "id":4, "content":"four"];
在get请求中,我尝试如下响应:

app.get('/api/texts/', function (req, res, next) {
    res.json(texts.slice(3, 1));
    next();
});
但是这个用[]来回应,没有别的。知道为什么吗

编辑:


上面正确地打印出
文本中的所有元素
。就在我将其更改为
res.json(text.slice(3,1))时它打印出数组。切片(x,y)不能有x 数组。切片(x,y)不能有x 
1
小于
3
@Andy I在使用执行get请求之前,使用push请求将元素推送到数组中。
[…'12345'].切片(3,1)/[]
预期的结果是什么?要获取数组的最后N个索引(不包括数组的第一个索引),请使用for循环。。。来吧链接问题中的标记答案使用
Math.max
从两个数字中选择一个数字。您的代码没有这样做。
1
小于
3
@Andy I在使用执行get请求之前,使用push请求将元素推送到数组。
[…'12345'].切片(3,1)/[]
预期的结果是什么?要获取数组的最后N个索引(不包括数组的第一个索引),请使用for循环。。。来吧链接问题中的标记答案使用
Math.max
从两个数字中选择一个数字。你的代码不能做到这一点。
app.get('/api/texts/', function (req, res, next) {
    res.json(texts);
    next();
});