删除数组的第一项(如从堆栈中弹出) [英] Remove first Item of the array (like popping from stack)

查看:17
本文介绍了删除数组的第一项(如从堆栈中弹出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过 ng-repeat 创建的项目列表.我也有删除按钮.点击删除按钮将数组的最后一项一项一项删除.Plunker

I have list of items created via ng-repeat. I also have Delete button. Clicking delete button removes last item of the array one by one. Plunker

但我想从第一个项目开始一个一个地删除项目.我怎样才能做到这一点?我用它来删除列表项:

But I want to remove items one by one starting from the first item. How can I do that? I used this for removing list Items:

  $scope.index = 1;
  $scope.remove = function(item) { 
    var index = $scope.cards.indexOf(item);
    $scope.cards.splice(index, 1);     
  }

有什么办法可以从顶部移除吗?

Is there any way I can remove from the top?

推荐答案

最简单的方法是使用 shift().如果您有一个数组,shift 函数会将所有内容向左移动.

The easiest way is using shift(). If you have an array, the shift function shifts everything to the left.

var arr = [1, 2, 3, 4]; 
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]

这篇关于删除数组的第一项(如从堆栈中弹出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆