gpt4 book ai didi

javascript - 如何使用 .shift() 返回从数组中取出的值

转载 作者:行者123 更新时间:2023-11-28 14:57:41 25 4
gpt4 key购买 nike

我正在尝试从 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));

最佳答案

只需返回arr.shift();

示例:

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

关于javascript - 如何使用 .shift() 返回从数组中取出的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42238864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com