Javascript 如何使用.shift()返回从数组中提取的值

Javascript 如何使用.shift()返回从数组中提取的值,javascript,Javascript,我试图从数组中提取arr.shift()中的值,我需要返回该值,我将如何执行此操作 function nextInLine(arr, item) { // Your code here arr.push(item); arr.shift(); return arr; // Change this line } // Test Setup var testArr = [1, 2, 3, 4, 5]; // Display Code console.log("Before:

我试图从数组中提取
arr.shift()
中的值,我需要返回该值,我将如何执行此操作

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  arr.shift(); 

  return arr; // Change this line
}

// Test Setup
var testArr = [1, 2, 3, 4, 5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

将值赋给变量

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  var tmp = arr.shift(); 

  return tmp;
}
或者

function nextInLine(arr, item) {
  // Your code here
  arr.push(item); 

  return arr.shift();
}

将值赋给变量

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  var tmp = arr.shift(); 

  return tmp;
}
或者

function nextInLine(arr, item) {
  // Your code here
  arr.push(item); 

  return arr.shift();
}

只需返回
arr.shift()

示例:

function nextInLine(arr, item) {
    arr.push(item);
    return arr.shift();
}

只需返回
arr.shift()

示例:

function nextInLine(arr, item) {
    arr.push(item);
    return arr.shift();
}

默认情况下,array shift删除数组的第一个元素,并返回相同的元素。所以,正如前面的回答中提到的,“return arr.shift()”将返回从数组中移除的元素


注意:shift()类似于array的pop()函数。唯一的区别是,pop删除具有数组最后一个索引(数组长度-1)的元素,而shift删除具有第一个索引(0)的元素。但是,pop和shift返回被删除的元素。

默认情况下,array shift删除数组的第一个元素,并返回相同的元素。所以,正如前面的回答中提到的,“return arr.shift()”将返回从数组中移除的元素

注意:shift()类似于array的pop()函数。唯一的区别是,pop删除具有数组最后一个索引(数组长度-1)的元素,而shift删除具有第一个索引(0)的元素。但是,pop和shift返回被删除的元素。

返回arr.shift()
以在一行中执行此操作。对于当前代码,需要将
.shift()
的结果存储在变量中,然后返回变量返回该值是什么意思?从函数返回或返回到数组?
Return arr.shift()
在一行中执行。对于当前代码,需要将
.shift()
的结果存储在变量中,然后返回变量返回该值是什么意思?从函数返回还是返回到数组?