1
我正在推動數組中的值。如何根據位置推送數組中的值?
我的確切的情況:
如果數組包含2個值,按壓操作推入值具有後進來的2個值之間(這是基於位置)。
我以某種方式使用彈出最後一個值,並用popp值推送新值。以下是我的代碼。
var fruits = ["Banana", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
var pop = fruits.pop();
fruits.push("Kiwi",pop);
document.getElementById("demo").innerHTML = fruits;
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to add a new element to the array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
我的代碼是工作Fine.But是有隻使用Push方法的任何更好的解決方案(不使用POP)。
在此先感謝。