簡體   English   中英

如何從javascript中的數組中刪除項目? 我想要像拉力之類的東西來做推力的相反

[英]How can I remove items from an array in javascript ? I want something like a pull to do the opposite of push

我希望我的應用程序在屏幕上有一個跟蹤活動的區域。

我創建了以下內容:

$scope.activity = [];

我在想的是,當某些事情開始時,我將像這樣推入這個數組:

$scope.activity.push("Loading content 1");
$scope.activity.push("Loading content 2");
$scope.activity.push("Loading content 3");
$scope.activity.push("Loading content 4");

然后,我可以在屏幕的某個區域顯示ng-repeat所發生的情況,該序列顯示數組中的所有內容:

<div ng-repeat="row in activity">
    {{ row }}
<div>

我的問題是,我不確定活動完成后如何從陣列中刪除項目。 有人可以給我一個建議,我該怎么做。 我真正需要的是某種pull函數,在其中可以指定所推送內容的名稱並將其刪除。 就像是:

 $scope.activity.pull("Loading content 4");

我還需要另一個功能,例如:

 $scope.activity.update("Loading content 4", status);

我正在尋找不使用jQuery或下划線的解決方案。 Myusers是IE9及更高版本。

使用數組拼接方法

更改數組的內容,在刪除舊元素的同時添加新元素。

您可以這樣做:

var activityArray = [];
activityArray.push("Loading content 1");
activityArray.push("Loading content 2");
activityArray.push("Loading content 3");
activityArray.push("Loading content 4");

//find the item we want to delete
var index = activityArray.indexOf('Loading content 4');// returns 3
activityArray.splice(index,1)//remove the item at index 3

.pop().shift()通常與.push()一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM