我有一个数组,目前它已被过滤到仅包括今天的游戏后,从阵列中获得前6个项目。
fixtures.filter(fixture => {
return fixture.date === today;
}).slice(0, 6);
这工作正常,但我希望在过滤后从数组中获取最后6个项目。我尝试了以下但它对我不起作用。
fixtures.filter((fixture, index, arr) => {
return fixture.date === today;
}).slice(Math.max(arr.length - 6, 1));
据说arr
未定义。有没有办法在一个链中做到这一点?
您可以使用.slice(-6)
来获取数组的最后6个元素。 A negative index can be used, indicating an offset from the end of the sequence。
fixtures.filter(fixture => {
return fixture.date === today;
}).slice(-6);