在本教程中,让我们学习一些JavaScript的基础知识。如何从一个数组中删除元素?下面的列表中有一些语言的例子。
- PHP - array_splice(offset)
- Python - inputArray.remove(offset)按索引移除项目。
这篇文章给出了代码来学习如何在JavaScript中做到这一点。它有很多从JavaScript数组中移除元素的例子。
- 通过索引或值从JavaScript数组中删除一个元素。
- 从JavaScript数组中按值删除所有匹配的元素。
- 使用过滤器从JavaScript数组中删除元素(一种替代方法)。
- 从数组中删除第一个元素javascript。
- 从数组中删除最后一个元素。
- 从JavaScript数组中移除一个元素(通过索引和值)
这个快速的例子得到了给定元素的第一个索引。然后,它通过发送第一个索引的位置来应用JavaScriptsplice()。*splice()*删除了指定索引中的项目。
快速例子
// this JavaScript example removes first occurrence of the matching element from array
const elements = [2, 5, 4, 5, 6, 5, 8];
console.log(elements);
// returns the index of the first match of value '5' from an array
const index = elements.indexOf(5);
// when the element is found the index will be non-negative
if (index > -1) {
// the second parameter '1' asks to remove one element
elements.splice(index, 1);
}
// result array after delete is [ 2, 4, 5, 6, 5, 8 ]
console.log(elements);
这个屏幕截图显示了上述例子的输出。它首先显示的是原始数组,然后是删除一个项目后的修改后的数组。
- 从JavaScript数组中按值删除所有匹配元素
这个例子创建了一个自定义的JavaScript函数来删除所有给定元素的出现。它在一个循环中迭代了所有的数组元素。
在每次迭代中,它通过当前索引比较并调用array.splice()。在PHP中,通过使用*array_diff()*函数,只需一行就可以删除所有出现的元素。
remove-all-item.html
<html>
<head>
<title>JavaScript Remove All Matching Element from Array</title>
</head>
<body>
<h1>JavaScript Remove All Matching Element from Array</h1>
<script>
function removeAllItem(elementsArray, element) {
var i = 0;
// iterate the elements array and remove matching element
// till the end of the array index
while (i < elementsArray.length) {
if (elementsArray[i] === element) {
elementsArray.splice(i, 1);
} else {
++i;
}
}
return elementsArray;
}
// this JavaScript example removes all occurence of the matching element from array
const elements = [ 2, 5, 4, 5, 6, 5, 8 ];
console.log(elements);
elementAfterRemoval = removeAllItem(elements, 5);
console.log(elementAfterRemoval);
</script>
</body>
</html>
输出
Original Array: (7) [2, 5, 4, 5, 6, 5, 8]
Output Array: (4) [2, 4, 6, 8]
- 使用过滤器从JavaScript数组中移除元素(一种替代方法)
这是一个替代方法,在移除一个项目后,返回相同的数组输出结果。
它不是一个循环,而是通过使用一个JavaScript过滤器来解析输入数组。过滤器回调检查条件,找到要删除的元素匹配。
如果没有找到匹配,当前元素将被推送到输出数组中。
remove-alternate.html
<html>
<head>
<title>JavaScript Remove Element from Array - Alternate Method
using filter</title>
</head>
<body>
<h1>JavaScript Remove Element from Array - Alternate Method
using filter</h1>
<script>
const elements = [ 2, 5, 4, 5, 6, 5, 8 ];
console.log(elements);
var value = 5
// filter function does not change the original array
// but the splice function changes the original array
newElements = elements.filter(function(item) {
return item !== value
})
console.log(newElements)
// result is [ 2, 4, 6, 8 ]
</script>
</body>
</html>
输出
Original Array: (7) [2, 5, 4, 5, 6, 5, 8]
Output Array: (4) [2, 4, 6, 8]
- 从数组中删除第一个元素 javascript
在JavaScript中,*array.shift()*函数会移除一个输入数组的第一个元素。*shift()*函数返回删除的元素,在这个例子中是2。
remove-first-element.html
<html>
<head>
<title>JavaScript Remove First Element from Array</title>
</head>
<body>
<h1>JavaScript Remove First Element from Array</h1>
<script>
// the JavaScript shift function moves elements to the left
// this is like pop from a stack
// splice function can also be used to achieve this
var elements = [ 2, 5, 4, 5, 6, 5, 8 ];
console.log(elements);
// removedElement is 2
var removedElement = elements.shift();
// result array after delete is [ 5, 4, 5, 6, 5, 8 ]
console.log(elements);
</script>
</body>
</html>
输出
Original Array: (7) [2, 5, 4, 5, 6, 5, 8]
Output Array: (6) [5, 4, 5, 6, 5, 8]
5)用JavaScript删除数组中的最后一个元素
JavaScript有一个函数*array.pop()来移除数组的最后一个元素。它也会像array.shift()*一样,将删除的项目返回给调用点。
注意:如果输入的数组是空的,那么shift()和pop()将返回未定义。
remove-last-element.html
<html>
<head>
<title>JavaScript Remove Last Element from Array</title>
</head>
<body>
<h1>JavaScript Remove Last Element from Array</h1>
<script>
// the JavaScript pop function removes last element from an array
var elements = [ 2, 5, 4, 5, 6, 5, 8 ];
console.log(elements);
// removedElement is 8
var removedElement = elements.pop();
// result array after delete is [ 2, 5, 4, 5, 6, 5 ];
console.log(elements);
</script>
</body>
</html>
输出
Original Array: (7) [2, 5, 4, 5, 6, 5, 8]
Output Array: (6) [2, 5, 4, 5, 6, 5]
这个例子在JavaScript中创建了自定义函数来删除所有指定元素的出现。相反,在JavaScript中应该有一个本地函数来做这件事。PHP、Python和大多数语言都有这样的本地函数。