📅  最后修改于: 2023-12-03 14:40:09.516000             🧑  作者: Mango
last()
方法last()
方法是 Collect.js 库中的一个方法,用于返回集合中的最后一个元素。
collect([1, 2, 3]).last();
last()
方法返回集合中的最后一个元素。
如果调用集合为空,则返回 undefined
。
下面是使用 last()
方法的一些示例:
const collect = require('collect.js');
const collection1 = collect([1, 2, 3]);
const collection2 = collect([]);
console.log(collection1.last()); // 输出: 3
console.log(collection2.last()); // 输出: undefined
可以看到,当集合非空时,last()
方法返回最后一个元素;当集合为空时,last()
方法返回 undefined
。
last()
方法返回集合中的最后一个元素,如果集合为空,则返回 undefined
。