js删除数组里指定的元素
js删除数组里指定的元素
首先可以给JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为:
Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; };
然后使用通过得到这个元素的索引,使用js数组自己固有的函数去删除这个元素:
Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } };
这样就构造了这样一个函数,比如我有有一个数组:
var emp = ['abs','dsf','sdf','fd']
假如我们要删除其中的fd,就可以使用:
emp.remove('fd');
原文链接:http://caibaojian.com/js-splice-element.html
js删除数组里指定的元素的更多相关文章
- js删除数组里的某个元素
首先可以给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为: Array.prototype.indexOf = function(val) { for (var i = ...
- Javascript删除数组里的某个元素
删除array数组中的某个元素,首先需要确定需要删除元素的索引值. ? 1 2 3 4 5 6 7 var arr=[1,5,6,12,453,324]; function indexOf(val){ ...
- js删除数组中重复的元素
1.方法一 将数组逐个搬到另一个数组中,当遇到重复元素时,不移动,若元素不重复则移动到新数组中 function unique(arr){ var len = arr.length; var resu ...
- js 删除数组的指定元素
//为数组对象增加相应的的方法,直接调用数组的removeByValue方法即可删除指定元素 Array.prototype.removeByValue = function (val) { for ...
- JS 删除数组中指定的某个元素的方法
//首先创建函数方法 Array.prototype.indexOf = function(val){ for(var i=0;i<this.length;i++){ if(this[i] == ...
- 用java来删除数组中指定的元素
public static void main(String[] args){ String[] a = new String[]{"1","5" ...
- 删除数组中指定的元素,然后将后面的元素向前移动一位,将最后一位设置为NULL 。 String[] strs={“aaa”,”ccc”,”ddd”,”eee”,”fff”,”ggg”}; 指定删除字符串“ccc”,把后的元素依次向前移动!!!
public static void main(String[] args) { int temp = -1; String[] strs = {"aaa", "ccc& ...
- javascript删除数组里的对象
Array.prototype.del = function(value) { //删除数组中指定的元素,返回新数组 function hasValue(array, value) { for(var ...
- js删除数组元素
一.清空数组 var ary = [1,2,3,4]; ary.splice(0,ary.length);//清空数组 console.log(ary); // 输出 [],空数组,即被清空了 二.删 ...
随机推荐
- 在RobotFramework--RIDE中把日期转化为整型进行运算
在RobotFramework--RIDE中把日期转化为整型进行运算 运行结果: 20180906 16:10:17.919 : INFO : ${time} = 2018-09-06 16:10:1 ...
- Python3 中hashlib及uuid的用法
Python3 中hashlib及uuid的用法: 可以生成随机ID import uuid import hashlib import time def creat_uuid(): return s ...
- 编译 ambari 2.7.3
官方给的教程比较简单,需要事先安装的工具也是这里列一点,那里列一点.在此记录一下编译要点(在 centos 7 下). 1. 事先需要安装的工具 yum install -y git svn node ...
- jmeter+Jenkins性能测试自动化搭建
一.安装java.ant.maven 1.官网下载tar.gz包 2.解压相应的tar包 3.配置/etc/profile路径 4.source /etc/profile 使配置生效. 二.安装Jen ...
- Codeforces Round #521 (Div. 3) C. Good Array
C. Good Array time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- HTTP Status 415 – Unsupported Media Type(使用@RequestBody后postman调接口报错)
1.问题描述:使用springMVC框架后,添加数据接口中,入参对象没使用@RequestBody注解,造成postman发起post请求, from-data可以调通接口,但是raw调不通接口,然后 ...
- Qt 学习之路 2(23):自定义事件
Qt 学习之路 2(23):自定义事件 豆子 2012年10月23日 Qt 学习之路 2 21条评论 尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如, ...
- winfom实现关闭后一直运行
using PLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...
- Margarite and the best present
Little girl Margarita is a big fan of competitive programming. She especially loves problems about a ...
- Codeforces 277E
按边建模,二叉树一条入边两条出边 判断就要用到mcmf的好处了 #include<bits/stdc++.h> using namespace std; const int maxn = ...