Skip to content

Instantly share code, notes, and snippets.

@aisin
Created July 16, 2014 11:44
Show Gist options
  • Save aisin/719c296c8ab8dfde1f29 to your computer and use it in GitHub Desktop.
Save aisin/719c296c8ab8dfde1f29 to your computer and use it in GitHub Desktop.
jQuery判断指定元素是否存在和删除数组中指定元素
/***********************************
*
* 判断指定元素是否存在
*
* *********************************/
var arr = ['a','b','c','d','e','f']
var str = 'b';
console.log( $.inArray(str , arr) );
//结果:
1
//说明:$.inArray(str , arr) 返回 str 在数组中的索引值,如果不存在返回 -1
/***********************************
*
* 删除数组中指定元素
*
* *********************************/
var arr = ['a','b','c','d','e','f']
arr.splice($.inArray('c' , arr),1);
console.log( arr );
//结果:
['a','b','d','e','f']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment