一、數組刪除指定位置元素
數組是一個非常基礎和常用的數據結構,它在JavaScript中也得到了廣泛的應用。在處理數組時,有時需要刪除數組中的某個元素,下面是刪除指定位置元素的代碼示例:
let arr = ['a', 'b', 'c', 'd', 'e']; let index = 2; arr.splice(index, 1); console.log(arr); // ["a", "b", "d", "e"]
通過splice()方法可以刪除數組中指定位置的元素。其中,第一個參數為要刪除的元素的索引,第二個參數為要刪除的元素個數。
二、JS數組刪除指定元素
如果要刪除JS數組中指定的元素,可以使用splice()或者filter()方法。splice()方法已經在一號小標題中講解過了,這裡主要介紹如何使用filter()方法進行刪除:
let arr = ['a', 'b', 'c', 'd', 'e']; let removeItem = 'c'; arr = arr.filter(item => item !== removeItem); console.log(arr); // ["a", "b", "d", "e"]
通過filter()方法過濾數組,返回一個新的數組,不包含被刪除的元素。
三、從數組中刪除指定元素
在Vue中,如果要從數組中刪除指定元素,可以使用splice()方法或Vue.set()方法:
// 使用splice()方法 this.items.splice(this.items.indexOf(item), 1); // 使用Vue.set()方法 Vue.set(this.items, this.items.indexOf(item), null); this.items = this.items.filter(item => item !== null);
以上代碼中,第一個示例使用splice()方法。this.items表示要刪除的數組,this.items.indexOf(item)表示要刪除的元素在數組中的位置。第二個示例中,使用Vue.set()方法把要刪除的元素位置的值設置為null。最後,使用filter()方法過濾掉空元素。
四、c數組刪除指定元素
c語言中,如果要刪除數組中指定的元素,需要採用將後面的元素依次往前移動,然後修改數組的長度來達到刪除的效果:
// 刪除數組中的第二個元素 int arr[5] = {1, 2, 3, 4, 5}; int index = 1; for (int i = index; i < 4; i++) { arr[i] = arr[i+1]; } arr[4] = 0; // 最後一個元素賦值為0
以上代碼中,使用for循環把要刪除的元素之後的元素往前移動一位,最後一個元素賦值為0。刪除元素後,需要手動修改數組的長度。
五、對象數組刪除指定元素
如果需要從對象數組中刪除指定元素,可以使用filter()方法進行刪除。以下是代碼示例:
var dataArr = [{id:0,name:'Mike'},{id:1,name:'Lee'},{id:2,name:'Vanessa'},{id:3,name:'Lucas'}]; var id = 2; dataArr = dataArr.filter(function(item){ return item.id != id; }); console.log(dataArr); // [{id:0,name:'Mike'},{id:1,name:'Lee'},{id:3,name:'Lucas'}]
以上代碼中,通過filter()方法過濾掉數組中id等於2的元素,返回刪除後的新數組。
六、數組怎麼刪除指定的元素
如果需要刪除JS數組中指定的元素,可以使用splice()或filter()方法進行刪除。下面是示例:
// 使用splice()方法 var arr = ['apple','orange','banana','kiwi']; var i = arr.indexOf('orange') if(i != -1) { arr.splice(i, 1) } console.log(arr) // ["apple", "banana", "kiwi"] // 使用filter()方法 var arr = ['apple','orange','banana','kiwi']; arr = arr.filter(function(item) { return item !== 'orange' }) console.log(arr); // ["apple", "banana", "kiwi"]
以上代碼中,使用splice()方法和filter()方法都可以刪除數組中的’orange’元素。
七、c#從數組中刪除指定元素
如果在C#中需要從數組中刪除指定元素,可以使用List類及其Remove()方法進行刪除:
List list = new List() { 1, 3, 5, 7, 9 }; int item = 5; if (list.Contains(item)) list.Remove(item); Console.WriteLine(list); // [1, 3, 7, 9]
以上代碼中,使用List類及其Remove()方法刪除數組中的值為5的元素。
八、C++刪除數組中指定元素
如果需要在C++中刪除數組中指定元素,可以使用vector容器及其erase()方法進行刪除:
vector arr{1, 3, 5, 7, 9}; int item = 5; for (auto it = arr.begin(); it != arr.end();) { if (*it == item) it = arr.erase(it); else ++it; } for (auto i : arr) cout << i << " "; // 1 3 7 9
以上代碼中,使用了vector容器及其erase()方法刪除了數組中的值為5的元素。
九、es6刪除數組指定元素
在ES6中,如果需要刪除數組中的指定元素,可以使用filter()方法或者ES6擴展運算符進行刪除。以下是代碼示例:
// 使用filter()方法 let arr = [1, 2, 3, 4, 5]; let item = 3; arr = arr.filter(i => i !== item); console.log(arr); // [1, 2, 4, 5] // 使用ES6擴展運算符 let arr = [1, 2, 3, 4, 5]; let item = 3; arr = arr.slice(0, arr.indexOf(item)).concat(arr.slice(arr.indexOf(item) + 1)); console.log(arr); // [1, 2, 4, 5]
以上代碼中,filter()方法和ES6擴展運算符都可以用來刪除數組中的值為3的元素,返回刪除後的新數組。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270210.html