删除数组中的某个元素 es6
时间: 2024-04-28 10:24:21 浏览: 99
要在ES6数组中删除某个指定元素,你可以使用findIndex()方法找到该元素的索引,然后使用splice()方法将其从数组中删除。
以下是一个例子:
```javascript
let array = [1,2,3,4,5];
let element = 3;
let index = array.findIndex(item => item === element);
if (index > -1) {
array.splice(index, 1);
}
console.log(array); // [1, 2, 4, 5]
```
在这个例子中,我们有一个数组array,想要删除的元素是3。我们使用findIndex()方法找到元素3的索引,然后使用splice()方法在数组中删除该元素。最后,我们打印出删除后的数组array,结果是。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [删除数组中某个指定元素或删除数组中某个对象元素](https://blog.csdn.net/weixin_44900104/article/details/125654679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [ES6数组中删除指定元素](https://blog.csdn.net/u013060778/article/details/122693869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文