代码之家  ›  专栏  ›  技术社区  ›  SilentDev

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

  •  0
  • SilentDev  · 技术社区  · 7 年前

    In a javascript array, how do I get the last 5 elements, excluding the first element?

    我正在使用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();
    });
    

    编辑:

    app.get('/api/texts/', function (req, res, next) {
        res.json(texts);
        next();
    });
    

    上面正确地打印出内部的所有元素 texts res.json(texts.slice(3,1)); 它打印出来了 [] .

    1 回复  |  直到 7 年前
        1
  •  2
  •   GifCo    7 年前

    数组。切片(x,y)不能有x<Y它将始终返回一个空数组。

    推荐文章