Rai*_*han 76 javascript angularjs
我有通过创建的项目列表ng-repeat.我也有删除按钮.单击"删除"按钮逐个删除阵列的最后一项.Plunker
但我想从第一个项目开始逐个删除项目.我怎样才能做到这一点?我用它来删除列表项:
$scope.index = 1;
$scope.remove = function(item) {
var index = $scope.cards.indexOf(item);
$scope.cards.splice(index, 1);
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以从顶部删除?
小智 164
最简单的方法是使用shift().如果你有一个数组,该shift函数将所有内容都移到左边.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
小智 26
只是用arr.slice(startingIndex, endingIndex).
如果未指定endingIndex,则返回从提供的索引开始的所有项目.
在你的情况下arr=arr.slice(1).
const a = [1, 2, 3]; // -> [2, 3]
// Mutable solutions: update array 'a', 'c' will contain the removed item
const c = a.shift(); // prefered mutable way
const [c] = a.splice(0, 1);
// Immutable solutions: create new array 'b' and leave array 'a' untouched
const b = a.slice(1); // prefered immutable way
const b = a.filter((_, i) => i > 0);
const [c, ...b] = a; // c: the removed item
Run Code Online (Sandbox Code Playgroud)
$scope.remove = function(item) {
$scope.cards.splice(0, 1);
}
Run Code Online (Sandbox Code Playgroud)
改变了..现在它将从顶部删除
| 归档时间: |
|
| 查看次数: |
98237 次 |
| 最近记录: |