Collect.js pop() 方法
pop() 方法用于从集合中删除最后一个元素并返回弹出的元素。
句法:
collect(array).pop()
参数: collect() 方法采用一个参数,该参数转换为集合,然后对其应用 pop() 方法。
返回值:此方法返回给定集合中弹出的元素。
下面的示例说明了 collect.js 中的 pop() 方法:
示例 1:
Javascript
const collect = require('collect.js');
const collection = collect(['Geeks', 'GFG', 'GeeksforGeeks']);
console.log("Popped Elements: " + collection.pop());
console.log("Collection Elements: " + collection.all());
Javascript
const collect = require('collect.js');
let obj = [
{
name: 'Rahul',
dob: '25-10-96'
},
{
name: 'Aditya',
dob: '25-10-96'
},
{
name: 'Abhishek',
dob: '16-08-94'
}
];
const collection = collect(obj);
console.log("Popped Element");
console.log(collection.pop());
console.log("Collection Elements");
console.log(collection.all());
输出:
Popped Elements: GeeksforGeeks
Collection Elements: Geeks,GFG
示例 2:
Javascript
const collect = require('collect.js');
let obj = [
{
name: 'Rahul',
dob: '25-10-96'
},
{
name: 'Aditya',
dob: '25-10-96'
},
{
name: 'Abhishek',
dob: '16-08-94'
}
];
const collection = collect(obj);
console.log("Popped Element");
console.log(collection.pop());
console.log("Collection Elements");
console.log(collection.all());
输出:
Popped Element
{ name: 'Abhishek', dob: '16-08-94' }
Collection Elements
[
{ name: 'Rahul', dob: '25-10-96' },
{ name: 'Aditya', dob: '25-10-96' }
]