shift()方法用於從集合中刪除第一個元素並返回它。
用法:
collect(array).shift()
參數:collect()方法采用一個參數,該參數將轉換為集合,然後在其上應用shift()方法。
返回值:此方法返回給定集合的第一個元素。
下麵的示例說明了collect.js中的shift()方法:
範例1:
Javascript
const collect = require('collect.js');
const collection = collect(['Geeks',
'GFG', 'GeeksforGeeks', 'Welcome']);
const shifted = collection.shift();
console.log(shifted);
console.log(collection.all());
輸出:
Geeks [ 'GFG', 'GeeksforGeeks', 'Welcome' ]
範例2:
Javascript
const collect = require('collect.js');
let obj = [
{
name:'Rahul',
marks:88
},
{
name:'Aditya',
marks:78
},
{
name:'Abhishek',
marks:87
}
];
const collection = collect(obj);
const shifted = collection.shift();
console.log(shifted);
console.log(collection.all());
輸出:
{ name:'Rahul', marks:88 } [ { name:'Aditya', marks:78 }, { name:'Abhishek', marks:87 } ]
相關用法
- JavaScript Array shift()用法及代碼示例
- Typescript Array shift()用法及代碼示例
- PHP Ds\Sequence shift()用法及代碼示例
- PHP Ds\Vector shift()用法及代碼示例
- PHP Ds\Deque shift()用法及代碼示例
- PHP SplDoublyLinkedList shift()用法及代碼示例
- Node.js shift()用法及代碼示例
- Lodash _.method()用法及代碼示例
- Node.js Http2ServerRequest.method用法及代碼示例
- Node.js http.IncomingMessage.method用法及代碼示例
- Javascript dataView.getInt16()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- JavaScript Math cosh()用法及代碼示例
- HTML DOM isEqualNode()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
注:本文由純淨天空篩選整理自AshokJaiswal大神的英文原創作品 Collect.js shift() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。