方法 | 说明 |
pop() | 此方法从数组末尾删除元素。 |
shift() | 就像pop()方法一样,它也会从数组的开头删除元素。 |
filter() | filter()方法以编程方式从数组中删除元素。 |
splice() | 此方法从特定索引中删除元素。 |
<html>
<body>
<script>
function removeLastElement() {
var shoeBrand = ["Nike", " Adidas", " Sparks", " RedTape"];
document.write("Elements in array before removing: <br>" + shoeBrand + "<br> <br>");
// Removing last element from the array
var poppedElement = shoeBrand.pop();
document.write("Removed element from array: " + poppedElement + "<br> <br>");
//display remaining elements present in array after removing
document.write("Elements present in array: <br>" + shoeBrand);
}
removeLastElement();
</script>
</body>
</html>
Removed element from array: RedTape
Elements present in array:
Nike, Adidas, Sparks
<html>
<body>
<script>
function removeElement() {
var shoeBrand = ["Nike", " Adidas", " Sparks", " RedTape"];
//initial length of the array
document.write("Elements in array before removing: <br>" + shoeBrand + "<br> <br>");
document.write("Array length before removing elements is:" + shoeBrand.length + "<br> <br>");
while (shoeBrand.length) {
//store removed element in a variable
var poppedElement = shoeBrand.pop();
//display removed element
document.write("Removed element from array: " + poppedElement + " <br>");
}
//Length of the array after removing all elements
document.write("<br> Array length after removing elements is:" + shoeBrand.length);
}
removeElement();
</script>
</body>
</html>
Array Length after removing elements is: 4
Removed element from array: RedTape
Removed element from array: Sparks
Removed element from array: Adidas
Removed element from array: Nike
Array Length after removing elements is: 0
<html>
<body>
<script>
function removeFirstElement() {
var shoeBrand = ["Nike", " Adidas", " Sparks", " RedTape"];
document.write("Elements in array before removing: <br>" + shoeBrand + "<br> <br>");
// Removing first element from the array
var poppedElement = shoeBrand.shift();
document.write("Removed element from array: " + poppedElement + "<br> <br>");
//display remaining elements present in array after removing
document.write("Elements present in array: <br>" + shoeBrand);
}
removeFirstElement();
</script>
</body>
</html>
Removed element from array: Nike
Elements present in array:
Adidas, Sparks, RedTape
<html>
<body>
<script>
function removeElement() {
var shoeBrand = ["Nike", " Adidas", " Sparks", " RedTape"];
//initial length of the array
document.write("Elements in array before removing: <br>" + shoeBrand + "<br> <br>");
document.write("Array length before removing elements is:" + shoeBrand.length + "<br> <br>");
while (shoeBrand.length) {
//store removed element in a variable
var poppedElement = shoeBrand.shift();
//display removed element
document.write("Removed element from array: " + poppedElement + " <br>");
}
//Length of the array after removing all elements
document.write("<br> Array length after removing elements is:" + shoeBrand.length);
}
removeElement();
</script>
</body>
</html>
Array Length after removing elements is: 4
Removed element from array: Nike
Removed element from array: Adidas
Removed element from array: Sparks
Removed element from array: RedTape
Array Length after removing elements is: 0
<html>
<body>
<script>
function removeElement() {
var shoeBrand = ["Nike", " Adidas", " Sparks", " RedTape", " Bata"];
document.write("Elements in array before removing: <br>" + shoeBrand + "<br> <br>");
// Removing first element from the array
var poppedElement = shoeBrand.splice(1, 3);
document.write("Removed element from array: " + poppedElement + "<br> <br>");
//display remaining elements present in array after removing
document.write("Elements present in array: <br>" + shoeBrand);
}
removeElement();
</script>
</body>
</html>
Removed element from array: Adidas, Sparks, RedTape,
Elements present in array:
Nike, Bata
<html>
<body>
<script>
function removeElement() {
var clothingBrand = ["Gucci", " Chanel", "Gucci", " Zara"];
// for loop to trace the whole array
for (var i = 0; i < clothingBrand.length; i++) {
//Match the specific element in array
if (clothingBrand[i] === "Gucci") {
//remove the matched element from array
var delEle = clothingBrand.splice(i, 1);
document.write("<br> Removed element: " + delEle);
document.write("<br> Remaining elements: " + clothingBrand);
document.write("<br>"); }
}
}
removeElement();
</script>
</body>
</html>
Removed element: Gucci
Remaining Element: Chanel, Zara
<script>
var clothingBrand = ["Gucci", " Chanel", " Calvin Klein", " Zara"];
document.write("Elements in array: " + clothingBrand);
//remove all elements
clothingBrand.splice(0, clothingBrand.length);
document.write("<br> Remaining elements: " + clothingBrand);
</script>
<html>
<body>
<script>
function isEven( value ) {
if(value%2 == 0)
return value;
}
//initialize the array named ary
var ary = [43, 243, 56, 24, 1021, 348].filter( isEven );
document.write("Even elements in array: " + ary);
</script>
</body>
</html>
<html>
<body>
<script>
//declare and initialize an array
var clothingBrand = ["Gucci", " Calvin Klein", " Chanel", " Zara"];
document.write("Elements in array: " + clothingBrand);
//delete element of index 1 from clothingBrand array
var result = delete clothingBrand[1];
//if returned value is true, element is deleted successfully
document.write("<br> Removed successfully: " + result + "<br>");
document.write("Remaining elements in array: " + clothingBrand);
</script>
</body>
</html>
<html>
<body>
<script>
//declare and initialize an array
var originalArray = ["Gucci", " Calvin Klein", " Chanel", " Zara"];
document.write("Initially elements in array: " + originalArray);
//declare one more array to keep the elements of original array
var newArray = originalArray
//clear the initially declared array
originalArray = [ ]
//display element of original and new array after removing
document.write("<br> <br> Array after removing elements: " + originalArray);
document.write("<br> <br> Elements in new array: " + newArray);
</script>
</body>
</html>
Array after removing elements:
Elements in new array: Gucci, Calvin Klein, Chanel, Zara
<html>
<body>
<script>
//declare and initialize an array
var array1 = ["Gucci", " Calvin Klein", " Chanel", " Zara"];
document.write("Initially elements in array: " + array1);
//set length of array to 0
array1.length = 0;
//display element of original and new array after removing
document.write("<br> <br> Array after removing elements: " + array1);
</script>
</body>
</html>
Array after removing elements: