配列の最初の値を取り出す Array.shift
ひとことで言うと
配列から最初の要素を削除し、その値を返します。このメソッドは、配列の長さを変更します。
ブロックとコード
配列から最初の値を削除して返す
ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。
[""].shift()[""].shift()使用例
If you have an array with 4 numbers, like 1, 2, 3, and 4, you remove the first number from the array by shifting it from the front. To shift the number from the array in blocks, do this:
JavaScript
let thoseNumbers = [1, 2, 3, 4];
let firstNumber = thoseNumbers.shift();Python
thoseNumbers = [1, 2, 3, 4]
firstNumber = thoseNumbers.shift()Find out how many odd numbers there are as you remove all the numbers from a list.
JavaScript
let odds = 0;
let ints = [45, 78, 98, 3, 5, 6, 12, 643, 0, 34, 4];
while (ints.length > 0) {
if ((ints.shift() % 2) > 0) {
odds++;
}
}Python
odds = 0
ints = [45, 78, 98, 3, 5, 6, 12, 643, 0, 34, 4]
while len(ints) > 0:
if (ints.shift() % 2) > 0:
odds += 1