欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数组和字符串的方法整合

程序员文章站 2023-11-09 21:24:10
js中的数组和字符串有点类似,不是说本质上,而是在遍历,获取时的类似。从标识来说,可以一眼看出那个是数组,那个是字符串;但在使用遍历时,会不经意的将两者混淆,导致方法用错。同时两者的方法又有好几个相同的,但需注意语义,以及有些方法是不会对原数组产生影响的。以下是我整理的一些关于数组和字符串的一些方法 ......

js中的数组和字符串有点类似,不是说本质上,而是在遍历,获取时的类似。从标识来说,可以一眼看出那个是数组,那个是字符串;但在使用遍历时,会不经意的将两者混淆,导致方法用错。同时两者的方法又有好几个相同的,但需注意语义,以及有些方法是不会对原数组产生影响的。以下是我整理的一些关于数组和字符串的一些方法,不保证完整性。

数组方法

arr.push()      可向数组的末尾添加一个或多个元素,并返回新的长度。会影响原数组

1 var a = [1,2];
2 a.push(3);
3 console.log(a); // [1,2,3]
4 console.log(a.push(8));  //4

arr.unshift()  在数组开头添加元素,返回该元素值。会影响原数组

1 var a = [1,2,3,"hello"];
2 console.log(a.unshift("world")); // world
3 console.log(a); // ["world",1,2,3,"hello"]

arr.pop()  用于弹出数组最后一个元素,返回该元素的值。会影响原数组。(等同于在数组中删除此元素)

1 var a = [1,2];
2 console.log(a.pop()); // 2
3 console.log(a); // [1]

arr.shift()  将数组的第一个元素从中删除,并且返回第一个元素的值。会影响原数组。(删除操作)

var a = [1,2];
console.log(a.shift()); // 1
console.log(a); // [2]

arr.concat()  用于连接两个或者多个数组,返回一个新的数组。不会影响原数组。

1 var arr = [1,2,3,4];
2 alert(arr.concat(1,2,3,[1,2,3],5).length); //11
3 arr.concat(1,2,4,[1,2,3],5);
4 alert(arr.length)    //4

arr.join()  接收一个参数作为分隔符,以字符串的形式返回。不会影响原数组。它接受0参数或者1个参数,若是传了多个参数只有第一个是有效的。参数若是为无,默认为','

1 [1,2,3,4].join('0').split('')    //["1", "0", "2", "0", "3", "0", "4"]
2 var arr = [1,2,3,4]; arr.join("&");
3 console.log(arr)    //[1,2,3,4]

 

arr.slice(n,m):用于切割并且创造一个新的数组,不会影响原数组。

  • 1个参数: 从开始位置截取到结尾
  • 2个参数: 从开始位置截取到结束位置 [a, b) 不包括结束位置本身 
  • 结束位置还可以是负数( 实际计算为:该负数 + 数组长度) 结束位置小于起始位置,返回 空数组 []
1 var a = [1, 2, 3];
2 console.log(a.slice(1));      // [2, 3]
3 console.log(a.slice(1, 2));      // [2]
4 console.log(a);       // [1, 2, 3]   原数组没有被改变
5 console.log(a.slice(1, -1));     // (-1 + 3) = 2 > a.slice(1, 2) > [2]
6 console.log(a.slice(1, -100));     // []

arr.splice(n,m,data)  用于截取数据,插入数据,删除数据。操作的是数据本身,所以会影响原数组。

  • [开始位置]:当只有1个参数的时候:从数组的开始位置删掉剩下的所有数据,返回删掉的数据。
  • [开始位置,截断的个数](注意,这和 slice 的第二个参数意义可是不一样的,这是 长度):以 n为起点,m为长度,删掉 [n, n+ m) 的元素
  • [开始位置,截断个数,插入的数据]: (n, m, data): 以 n为起点, m为长度,删掉 [n, n+ m] 的元素,并填充 data 到删除的位置
1 var array = [15, 5, 10, 2, 0];
2 console.log(array.splice(1, 3,1,2,1));  //5,10,2
3 console.log(array);     //15, 1, 2, 1, 0
4 console.log(array.splice(1, 3));  //5,10,2
5 console.log(array);     // [15,0]
6 console.log(array.splice(1));     // 0
7 console.log(array);     //15

arr.reverse()  将数组元素倒顺,会影响原数组。

1 var a = [1,2,3,5];
2 console.log(a.reverse());  // [5,3,2,1]

arr.sort(fn)  排序方法,参数:fn(a,b):比较函数,无参数的时候按照字母表 ascii 升顺排序

1 var a = [1, 2, 3, 5, 10, 25];
2 console.log(a.sort());     // [1, 10, 2, 25, 3, 5]

sort()默认对每一个 子项 调用转型方法 tostring(),之后才进行判断。而这个时候其实是根据 ascii 码的大小来判断的。因此 "15" < "5"。先比较第一位,再比较第二位。想要按我们预期的排序,需进行一些处理。

1 var array = [15, 5, 10, 2, 0];
2 array.sort(function(a,b){
3         return a-b;
4 })
5 console.log(array);  //[0, 2, 5, 10, 15]

以下为es5新增方法(新增的方法都不会影响原数组)

arr.indexof()  返回在该数组中第一个找到的元素位置,若不存在则返回 -1

1     var array = [15, 5, 10, 2, 0];
2     console.log(array.indexof(1));       //-1
3     console.log(array.indexof(15));     //0

arr.foreach():对数组中的每一项运行给定函数,这个方法没有返回值(遍历数组,获得数组所有元素)参数为function类型,默认有传参(遍历数组内容,对应数组索引,数组本身)

1     var array = [15, 5, 10, 2, 0];
2     array.foreach(function (value, index, array) {
3         console.log(array[index]); 
4     });

arr.map():对数组中的每一项运行给定函数,返回每次调用结果组成的数组。

1     var array = [15, 5, 10, 2, 0];
2     var num = array.map(function (value, index, array) {
3         return value * 3;
4     });
5     console.log(array, num);      //[15, 5, 10, 2, 0] ,[45, 15, 30, 6, 0]

arr.filter():对数组中的每一项运行给定函数,返回该函数会返回 true 的项组成的数组(筛选)

1     var array = [15, 5, 10, 2, 0];
2     var num = array.filter(function (value, index, array) {
3         return value >0;
4     });
5     console.log(array, num);         //[15, 5, 10, 2, 0] ,[15, 5, 10, 2]

两个归并的方法

  • arr.reduce(function(pre, next, index, array) {}) 从左到右
  • arr.reduceright(function(pre, next, index, array) {}) 从右到左

some: 对数组中的每一项运行给定函数,如果函数对任一项返回 true,就会返回 true,否则 false

every:对数组中的每一项运行给定函数,如果函数对所有的项都返回 true,才返回 true,否则 false

以上就是我所知晓的所有数组方法,es6还未涉及到,后续补充!!

字符串方法

indexof(data,start)   用于返回某个数组或者字符串中规定字符或者字符串的位置;当前查询字符所在的位置的下标,如无,返回-1,start表示从第几位开始查询。

1     var str="hello world!"
2     console.log(str.indexof("h"));    //0
3     console.log(str.indexof("i"));    //-1

str.lastindexof()  判断一个字符最后一次出现在某个字符串的索引,如果包含返回它的索引,如果不包含返回-1.

1     var str="hello world!"
2     console.log(str.lastindexof("d"));    // 10
3     console.log(str.lastindexof("i"));    //-1

str.charat()    返回指定位置的字符

1     var str="hello world!"
2     console.log(str.charat(1));    //e
str.substr(n,m)  从索引n开始,截取m个字符,将截取的字符返回,对原字符串没有任何改变
1     var str="hello world!"
2     console.log(str.substr(1));    //ello world!
3     console.log(str.substr(1,3))   //ell

str.substring(n,m)  返回从指定位置n,到结束位置(不含)m 的字符串,如果不指定结束位置,则从开始位置到结尾

1     var str="hello world!"
2     console.log(str.substring(1));    //ello world!
3     console.log(str.substring(1,3))   //el

str.slice(n,m)  同substring,需要注意和数组中方法slice()的相似

1     var str="hello world!"
2     console.log(str.slice(1));    //ello world!
3     console.log(str.slice(1,3))   //el

str.split("-")  通过指定字符分割字符串,返回一个数组

1     var str="hello world!"
2     console.log(str.split(" "));    // ["hello", "world!"]

str.replace("需要替换的字符串","替换之后的字符串")  将字符串中的一些字符替换为另外一些字符。最好配合正则使用

 
1     var str="hello world!"
2     console.log(str.replace("l","*"));    // he*lo world!

str.charcodeat()   返回指定索引出的unicode字符

1     var str="hello world!"
2     console.log(str.charcodeat(0));    // 104

str.concat()    拼接2个字符串,返回一个新字符串,对原有字符串没有任何改变。

1     var str="hello world!"
2     console.log(str.concat("d"));    // hello world!d
3     console.log(str);   //hello world!

str.match()     可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。把找到的字符放在数组里,返回一个数组。

1     var str="hello world!"
2     console.log(str.match("d"));    // ["d", index: 10, input: "hello world!", groups: undefined]

str.search()     方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。

1     var str="hello world!"
2     console.log(str.search("d"));    // 10
3     console.log(str.search("l"));   //2

以上为字符串的方法,同数组,不涉及es6的!!

注意方法是否会影响原数组!!