js数组查找是否存在某个值

  1. indexOf(searchvalue,fromindex):返回某个指定的字符串值在字符串中首次出现的位置,没有出现返回-1。
  2. find(() => {}):用于找出第一个符合条件的数组成员,如果没有找到返回undefined。
  3. findIndex(() => {}): 和find()类似,返回第一个符合条件的数组成员的位置,如果没有符合的,返回-1。
  4. includes(searchvalue,fromindex):表示数组中是否包含指定的值,返回Boolen类型。 第二个参数如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
  5. filter(function(currentValue,index,arr), thisValue): 创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。注: a. 不会对空数组进行检测 b. 不会改变原始数组

利用filter()函数去除数组中的重复元素

	var arr = ["a", "c", "b", "c"];
	var arr1 = [];
	function removeSame(num,index) {
		// 方法1
		return arr.indexOf(num, index+1) == -1; 
		// 方法2
		/* return arr.indexOf(num) == index; */
	}
	arr1=arr.filter(removeSame)

你可能感兴趣的:(js,javascript)