繁体   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