最好的办法通过值删除数组元素 [英] The best way to remove array element by value

查看:107
本文介绍了最好的办法通过值删除数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组

arr = ["orange","red","black","white"]

我要增强界定其作用像这样的 deleteElem()方法数组对象:

arr2 = arr.deleteElem("red"); // ["orange","black","white"] (with no hole)

什么是完成这一任务的最好方法只使用值参数(没有索引)?

What is the best way to accomplish this task using just the value parameter (no index)?

先谢谢了。

推荐答案

下面是它是如何做的:

var arr = ["orange","red","black","white"];
var index = arr.indexOf("red");
if (index >= 0) {
  arr.splice( index, 1 );
}

这code会在你的阵列删除1 occurency红的。

This code will remove 1 occurency of "red" in your Array.

这篇关于最好的办法通过值删除数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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