本文翻译自:How to remove element from an array in JavaScript?

var arr = [1,2,3,5,6];

I want to remove the 1st element of the array so that it becomes: 我想删除数组的第一个元素,使其成为:

var arr = [2,3,5,6];

To extend this question, what if I want to remove the 2nd element of the array so that it becomes: 要扩展这个问题,如果我想删除数组的第二个元素,使其变为:

var arr = [1,3,5,6];

#1楼

参考:https://stackoom.com/question/8PHb/如何从JavaScript中删除数组中的元素


#2楼

The Array.prototype.shift method removes the first element from an array, and returns it. Array.prototype.shift方法从数组中删除第一个元素,并返回它。 It modifies the original array. 它修改了原始数组。

var a = [1,2,3]
// [1,2,3]
a.shift()
// 1
a
//[2,3]

#3楼

For a more flexible solution, use the splice() function. 要获得更灵活的解决方案,请使用splice()函数。 It allows you to remove any item in an Array based on Index Value: 它允许您根据索引值删除数组中的任何项:

var indexToRemove = 0;
var numberToRemove = 1;arr.splice(indexToRemove, numberToRemove);

#4楼

也许是这样的:

arr=arr.slice(1);

#5楼

shift() is ideal for your situation. shift()非常适合您的情况。 shift() removes the first element from an array and returns that element. shift()从数组中删除第一个元素并返回该元素。 This method changes the length of the array. 此方法更改数组的长度。

array = [1, 2, 3, 4, 5];array.shift(); // 1array // [2, 3, 4, 5]

#6楼

Wrote a small article about inserting and deleting elements at arbitrary positions in Javascript Arrays. 写了一篇关于在Javascript Arrays中的任意位置插入和删除元素的小文章。

Here's the small snippet to remove an element from any position. 这是从任何位置删除元素的小片段。 This extends the Array class in Javascript and adds the remove(index) method. 这扩展了Javascript中的Array类,并添加了remove(index)方法。

// Remove element at the given index
Array.prototype.remove = function(index) {this.splice(index, 1);
}

So to remove the first item in your example, call arr.remove(): 因此,要删除示例中的第一项,请调用arr.remove():

var arr = [1,2,3,5,6];
arr.remove(0);

To remove the second item, 要删除第二个项目,

arr.remove(1);

Here's a tiny article with insert and delete methods for Array class. 这是一篇包含Array类的insert和delete方法的小文章 。

Essentially this is no different than the other answers using splice, but the name splice is non-intuitive, and if you have that call all across your application, it just makes the code harder to read. 基本上这与使用splice的其他答案没有什么不同,但是名称splice是非直观的,如果你在整个应用程序中都有这个调用,它只会使代码更难阅读。

如何从JavaScript中删除数组中的元素?相关推荐

  1. Javascript循环删除数组中元素的3种方法

    本文主要跟大家分享了关于Javascript循环删除数组中元素的几种方法,分享出来供大家参考学习,下面与微点阅读小编一起来看看详细的介绍: 问题 大家在码代码的过程中,经常会遇到在循环中移除指定元素的 ...

  2. php数组重复值销毁,如何从PHP中删除数组中的重复值

    如何从PHP中删除数组中的重复值 如何从PHP中删除数组中的重复值? 21个解决方案 204 votes 使用array_unique(). 例: $array = array(1, 2, 2, 3) ...

  3. 从Ruby中删除数组中的重复元素

    本文翻译自:Remove duplicate elements from array in Ruby I have a Ruby array which contains duplicate elem ...

  4. JS中删除数组中的元素

    前言:主要记录一下数组在js中的使用问题. 一.基本操作 1.数组的创建 主要有两种方式: var arr1 = new Array(); var arr2 = [",",&quo ...

  5. java数组删除元素_java中删除 数组中的指定元素方法

    java中删除 数组中的指定元素要如何来实现呢,如果各位对于这个算法不是很清楚可以和小编一起来看一篇关于java中删除 数组中的指定元素的例子. java的api中,并没有提供删除数组中元素的方法.虽 ...

  6. c语言删除数组中的最小值,C语言中删除数组中某个元素的方法

    C语言中删除数组中某个元素的方法 发布时间:2020-06-17 14:22:39 来源:亿速云 阅读:1964 作者:鸽子 C语言实现删除数组中某个元素 大家知道C语言实现删除数组中某个元素方法吗? ...

  7. php js动态删除数组元素,javascript如何删除数组中的指定元素

    js删除数组中的指定元素主要分为两步,首先判断数组中是否包含这个元素,然后再通过splice()方法来删除指定元素 本篇文章主要介绍的是如何通过javascript语言对数组中的指定元素进行删除的方法 ...

  8. Javascript中删除数组中重复出现的元素

    今天在做到蓝桥云课中用js进行删除数组中重复出现的元素,自己进行了一点总结: 这里的删除相同元素与c++思想上不同. 在c++中我是通过先按大小排序后,再进行比对然后后往前赋值类似于整体移动前一位,长 ...

  9. 如何从值中删除数组中的项目?

    有没有一种方法可以从JavaScript数组中删除项目? 给定一个数组: var ary = ['three', 'seven', 'eleven']; 我想做类似的事情: removeItem('s ...

最新文章

  1. 【算法与数据结构】汉诺塔问题Java实现
  2. office 2013 安装问题
  3. 环形缓冲区ringbuffer
  4. [詹兴致矩阵论习题参考解答]习题1.3
  5. 从percona server 5.7换到mariadb 10.2
  6. sysbench数据库性能压测详解
  7. Co-Fusion: Real-time Segmentation, Tracking and Fusion of Multiple Objects
  8. 隐马尔可夫模型HMM[转载牛人,看了半天没看懂]
  9. 工业相机二次开发简约教程
  10. [转载]如何用C#做一个类似于桌面插件的程序
  11. win10误删IE浏览器文件如何恢复?
  12. [艾兰岛]编辑器做传送门——kura酱长期更新
  13. NSG2-一个很好用的ns2的tcl脚本自动生成软件
  14. Ubuntu18.04运行校园网客户端
  15. 【机器学习】浅谈正规方程法梯度下降
  16. shell脚本实例-交叉证认
  17. 数分可视化笔记整理4 - 2017年内地电影和票房数据情况(条形图)
  18. 解决OBS同时录制电脑音频和人声时出现的声音不同步问题
  19. 跨境电商shopee的本土开店经验
  20. Linux AT24C256芯片 数据手册解读

热门文章

  1. Hibernatel框架关联映射
  2. MessageBox函数
  3. [APEC中小企业峰会2009上]对话:经济适用男 vs 超级豪华男
  4. Scribe+HDFS日志收集系统安装方法
  5. Cobalt Strike 3.13的新功能
  6. 重装系统后遇到Git Authentication failed 错误
  7. 【Spring】---【AOP】
  8. [编织消息框架][网络IO模型]AIO
  9. git 使用http方式的一个小问题
  10. Zabbix---6 监控 端口 连接数