JavaScript中数组的22种方法必学(推荐)(4)

  splice()和slice()拥有非常相似的名字,但它们的功能却有本质的区别。splice()方法用于删除原数组的一部分成员,并可以在被删除的位置添加入新的数组成员,该方法会改变原数组

  splice()返回一个由删除元素组成的数组,或者如果没有删除元素就返回一个空数组

 splice()的第一个参数start指定了插入或删除的起始位置。如果start是负数,则start = max(length + start,0);如果start是NaN,则相当于start = 0

  如果只提供一个元素,相当于将原数组在指定位置拆分成两个数组

var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice());// [1,2,3,4,5,6,7,8] [] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(4));// [1,2,3,4] [5,6,7,8] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(-4));//-4+8=4; [1,2,3,4] [5,6,7,8] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(-9));//max(-9+8,0)=0 [] [1,2,3,4,5,6,7,8] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(NaN));//[] [1,2,3,4,5,6,7,8]

  第二个参数number指定了应该从数组中删除的元素的个数。如果省略第二个参数,从起始点开始到数组结尾的所有元素都将被删除。如果number是负数或NaN或undefined,则number=0,因此不删除元素

var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(0,2));// [3,4,5,6,7,8] [1,2] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(10,2));// [1,2,3,4,5,6,7,8] [] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(1,100));// [1] [2,3,4,5,6,7,8] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(1,-5));//[1,2,3,4,5,6,7,8] [] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(1,NaN));//[1,2,3,4,5,6,7,8] [] var a = [1,2,3,4,5,6,7,8]; console.log(a,a.splice(1,undefined));//[1,2,3,4,5,6,7,8] []

  如果后面还有更多的参数,则表示这些就是要被插入数组的新元素

var a = [1,2,3,4,5]; console.log(a,a.splice(2,0,'a','b'));//[1,2,'a','b',3,4,5] [] console.log(a,a.splice(2,2,[1,2],3));//[1,2,[1,2],3,3,4,5] ['a','b']

数组位置方法

  ES5为数组实例添加了两个位置方法:indexOf()、lastIndexOf()

【indexOf()】

  indexOf(search,start)方法接收search和start两个参数,返回search首次出现的位置,如果没有找到则返回-1

  search参数表示要搜索的项;使用严格相等运算符(===)进行比较

var arr = [1,2,3,'1','2','3']; console.log(arr.indexOf('2'));//4 console.log(arr.indexOf(3));//2 console.log(arr.indexOf(0));//-1

  start参数表示该搜索的开始位置,该方法会隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数字。若忽略该参数或该参数为undefined或NaN时,start = 0

var arr = ['a','b','c','d','e','a','b']; console.log(arr.indexOf('a',undefined));//0 console.log(arr.indexOf('a',NaN));//0 console.log(arr.indexOf('a',1));//5 console.log(arr.indexOf('a',true));//5 console.log(arr.indexOf('a',-1));//max(0,-1+7)=6; -1 console.log(arr.indexOf('a',-5));//max(0,-5+7)=2; 5 console.log(arr.indexOf('a',-50));//max(0,-50+7)=0; 0 var person = {name: 'Nicholas'}; var people = [{name: 'Nicholas'}]; var morePeople = [person]; alert(people.indexOf(person));//-1,因为person和people[0]虽然值相同,但是是两个引用 alert(morePeople.indexOf(person));//0,因为person和morepeople[0]是同一个引用 alert(morePeople.indexOf({name: 'Nicholas'}));//-1,因为不是同一个引用

  indexOf()方法兼容写法

if (typeof Array.prototype.indexOf != "function") { Array.prototype.indexOf = function (searchElement, fromIndex) { var index = -1; fromIndex = fromIndex * 1 || 0; for (var k = 0, length = this.length; k < length; k++) { if (k >= fromIndex && this[k] === searchElement) { index = k; break; } } return index; }; }

【lastIndexOf()】

  与indexOf()不同,lastIndexOf()从右向左查找

  lastIndexOf(search,start)方法接收search和start两个参数,返回search第一次出现的位置,如果没有找到则返回-1

  search参数表示要搜索的项;使用严格相等运算符(===)进行比较

var arr = [1,2,3,'1','2','3']; console.log(arr.lastIndexOf('2'));//4 console.log(arr.lastIndexOf(3));//2 console.log(arr.lastIndexOf(0));//-1

 start表示该搜索的开始位置,该方法会隐式调用Number()转型函数,将start非数字值(undefined除外)转换为数。若忽略该参数或该参数为undefined或NaN时,start = 0

  与字符串的lastIndexOf()方法不同,当search方法为负数时,search = max(0,length+search)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzsyzz.html