紀錄工作經驗、相關知識,解決技術相關問題。

Javascript

JS shift() 刪除 / 取得陣列第一筆資料

JavaScript shift() 用法

shift() 的用法,本來是用來刪除陣列中的第一筆資料,並將刪除的第一筆資料進行回傳。

const a = [ '1%', '#2', '@3', '55EE', 'ABC']
const b = a.shift()

console.log(a)
// expected output: ["#2", "@3", "55EE", "ABC"]

console.log(b)
// expected output: "1%"

但也因為會將第一筆資料回傳的特性,所以可以用在取得第一筆資料上。

JS 刪除陣列第一筆資料

const a = [ '1%', '#2', '@3', '55EE', 'ABC']
a.shift()
console.log(a)
// expected output: ["#2", "@3", "55EE", "ABC"]

JS 取得陣列第一筆資料

傳統做法

const a = [ '1%', '#2', '@3', '55EE', 'ABC']
console.log(a[0])
// expected output: "1%"

使用 shift()

const a = [ '1%', '#2', '@3', '55EE', 'ABC']
console.log(a.shift())
// expected output: "1%"

2 留言

  1. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/zh-TC/register?ref=V2H9AFPY

  2. At the beginning, I was still puzzled. Since I read your article, I have been very impressed. It has provided a lot of innovative ideas for my thesis related to gate.io. Thank u. But I still have some doubts, can you help me? Thanks.

發表迴響