js-常用数组方法总结

时间:2022-08-11 19:15:20

【前面的话】说数组“万能”,可能夸张了一点,但是就个人观点,数组的运用是非常广泛的,这里的广泛当然也包含了在其他语言中的运用。举例说明,在javascript当中,由于字符串的不可变性,在进行字符串拼接等操作中,都极大的消耗了内存,这时候用数组来“装载”字符串,操作简单,又不消耗过多的内存,真可谓是“香饽饽”。下面我总结了数组当中常用的一些操作。

——首先我们来对数组进行简单的介绍:

js-常用数组方法总结
//   var o = { };
// o.c 属性;
// o["c"] 键-值 的集合; 字典
// 这两个的效果是一样的 var array = [6,7,5,84,6];
//所有对象,都具有toString和valueOf
console.log(array.toString());//toSTring内部就调用join()
console.log(array.valueOf());//返回当前数组对象
js-常用数组方法总结

 1、 array.join();    // 将数组元素连接起来以构建一个字符串

1 // 将一个字符串数组输出为|分割的形式,比如“刘备|张飞|关羽”。使用两种方式实现
2 var array = ["刘备","张飞","关羽"];
3 console.log(array.join("|"));

2、 array.reverse();    // 颠倒数组中元素的顺序

1 //    将一个字符串数组的元素的顺序进行反转。["a","b","c","d]——>["d","c","b","a"]
2 var array = ["a","b","c","d"];
3 array.reverse();
4 console.log(array);

3、 array.filter();    // 使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组    //这里涉及到兼容旧环境的问题

js-常用数组方法总结
 1 var array = [1500,1200,2000,2100,1800];
2 //会把筛选的结果返回一个新数组
3 array = array.filter(function(element,index,thisObj){
4 //element 数组中的每一个元素
5 //index 索引
6 //thisObj 当前数组对象
7 if (element >= 2000) {
8 return false;
9 }
10 return true;
11 });
12 console.log(array);
js-常用数组方法总结
4、 array.indexOf();
            array.lastIndexOf();
js-常用数组方法总结
//    ["c","a","z","a","x","a"]找到数组中每一个a出现的位置,找不到返回-1
//indexOf 获取数组中第一个符合条件元素的位置
//lastIndexOf 获取数组中最后一个符合条件的元素的位置 var array = ["a","a","z","a","x","a"];
console.log(array.indexOf("a",2));
console.log(array.lastIndexOf("a"));
//
var index = -1;
do {
index = array.indexOf("a", index+1);
if (index != -1) {
console.log(index);
}
}while(index != -1);
js-常用数组方法总结

5、 array.sort();

js-常用数组方法总结
 1 //    ["c","a","z","a","x","a"]编写一个方法,求数组中每一个字符出现的次数
2 // 排序
3 var array = [3,6,1,5,10,2];
4 // array.sort();//如果不传参数,按默认的方式排序,按第一个字符的ascii码排序
5 // console.log(array);
6
7 //如果 compareFunction(a, b) 小于 0 ,那么 a 会被排列到 b 之前;
8 //如果 compareFunction(a, b) 等于 0 , a 和 b 的相对位置不变。备注: ECMAScript 标准并不保证这一行为,而且也不是所有浏览器都会遵守(例如 Mozilla 在 2003 年之前的版本);
9 //如果 compareFunction(a, b) 大于 0 , b 会被排列到 a 之前。
10 array.sort(function(a,b) {
11 return a - b;
12 });
13 console.log(array);
14
15 //调用自己模拟的sort
16 //sort(array, function(a,b) {
17 // return -(a - b);
18 //});
19 //console.log(array);
js-常用数组方法总结

6、 array.splice();  //插入、删除或替换数组的元素

var a = [1,2,3,4,5,6,7,8]
a.splice(4); // 返回 [5,6,7,8]; a is [1,2,3,4]
a.splice(1,2); // 返回 [2,3]; a is [1,4]
a.splice(1,1); // Netscape/JavaScript 1.2 返回 4 ,应该返回 [4]
a.splice(1,0,2,3); // Netscape/JavaScript 1.2 返回 undefined ,应该返回 []

7、 array.slice();  // 从当前数组截取一个新数组

var a = [1,2,3,4,5];
a.slice(0,3); // 返回 [1,2,3]
a.slice(3); // 返回 [4,5]
a.slice(1,-1); // 返回 [2,3,4]
a.slice(-3,-2); // 返回 [3]; IE 4存在的Bug: 返回[1,2,3]
8、 array.forEach();   //遍历数组 
                                    //  注意与filter的区别
                                    //这里涉及到兼容旧环境
js-常用数组方法总结
 1     var array = [4,6,7,8,3,46,8];
2
3 //forEach 遍历数组
4 // array.filter(function (item,index,obj) {
5 // })
6
7 for(var i = 0; i < array.length ; i++) {
8 var item = array[i];
9 }
10
11 array.forEach(function (item, index,obj) {
12 console.log(item);
13 console.log(index);
14 console.log(obj);
15 })
js-常用数组方法总结

【输出】

aa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3n5s1lhWp+Lz+MbrH7mWn75iir+yNmDkesPwkslkZWVlCCG9Xn/x4kWbzYbWelibTKaEhAST6Vdvea3RaKKjoyMiIgQCQVRUlMlkYlm2tLS0s7OToqjCwkJ+7md1VCkpKeXl5RKJxO12SyQSiGoH7NaJCj4qxsdMNk5ZtNZwjopl2YKCAsEvXnzxRe4wLJioKIpKS0vjnhf5r1Uqlfn5+RqNprm5mf3lVSgBG1xZWYmKiqqsrKRpmqKo9PR0OPzbAbt1Sn36jnplyoQQMs9YhmvGaRe9OiqWYae+V81KdQzNuC3ugYph7pWLNEkPVY9Zf7mOab1hwdv08I9/bsN9SRBEdXU1Ci4qj8dz/vz59vZ2t9tdW1t7+PBhbi1Jkjk5ORUVFVqtlh+8eqJCIpHcvXvX5/NJpdIzZ87ARMUO2K0nf7n5Bu4yP+eSC60zp0fZqdFbEz8m3+/OfqAfXuKX8y+85yYq1hsWpE2jUigU6enpFPX0vk1MTMTHxzscjiAP//r6+iIjI1966aW8vLyYmBh+bUtLS0pKCr9ZtFYnKysr3EQI9+TNfxVEtU12cVTc4d+e5XK5rl69OjQ05L9wS51AVNskvKLyfzEV96GoHAlYwr0sarujCph5v5fU2Vsg499Vhv/YjimNYEgkksjIyDt37rC/fk1/kGd14eTvtgqvqAB4DuyzAgCwgj0VAJhBVABgBlEBgBm2qP7whz/g2hQAuxpEBQBmEBUAmG0S1aKFKe/wBPOm4RAVAJxNorrVS71y2ZbeQPo2e4kGRAUAZ/PDv5qfPa9ctiXVujzejYZBVABwgnpOdXuAejXddvaG0+lZ90AQogKAA1EBgBkc/gGAGUxUAIAZTKkDgBmc/AUAM4gKAMwgKgAwg5d+AIDZ/wcWkpYSF1b8wQAAAABJRU5ErkJggg==" alt="" />

9、 html5提供的方法:
i)indexOf ;    lastIndexOf
ii)filter ;  forEach   

10、清空数组的三种方式:

js-常用数组方法总结
var array = [1, 2, 3, 4];
//方式1
array.splice(0,array.length); //删除数组中所有项
//方式2
array.length = 0; //length属性可以赋值,length是只读
//方式3
array = []; // 推荐
js-常用数组方法总结

js中数组Array的一些常用方法总结

var list = new Array()是我们在js中常常写到的代码,今天就总结一下Array的对象具有哪些方法。

  list[0] = 0;

  list[1] = 1;

  list[2] = 2;

  或者这样声明:var list = [0,1,2];

(1) shift():

   alert(list.shift());   //删除数组的第一个元素,返回删除的值。这里是0 
   alert(list.length);  //返回的数组长度是2

(2) unshift( i , j ):

list.unshift(3,4);  //把参数加载数组的前面,现在list:中是3,4,0,1,2

   alert(list.length);  //现在数组的长度是5

(3) pop():

list.pop();   //删除数组的最后一个元素,返回删除的值。这里是2
   alert(list.length);  //返回的数组长度是2

(4)push(param):

list.push(3);  //将参数加载到数组的最后,现在List中是:0,1,2,3

alert(list.length);   //返回的数组长度是4

(5)concat( i , j ):

alert(list.concat(5,6));  //将参数与之前的数组list拼接起来,现在的list是:0,1,2,5,6

alert(list.length);        //返回的数组长度是5

(6) splice(start,deleteCount,val1,val2,...):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...

(7) reverse:将数组反序 
  var a = [1,2,3,4,5]; 
  var b = a.reverse(); //a:[5,4,3,2,1] b:[5,4,3,2,1]

(8) sort(orderfunction):按指定的参数对数组进行排序 
  var a = [1,2,3,4,5]; 
  var b = a.sort(); //a:[1,2,3,4,5] b:[1,2,3,4,5]

(9) slice(start,end):返回从原数组中指定开始下标到结束下标之间的项组成的新数组 
  var a = [1,2,3,4,5]; 
  var b = a.slice(2,5); //a:[1,2,3,4,5] b:[3,4,5]

(10) join(separator):将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符 
  var a = [1,2,3,4,5]; 
  var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"

-----转

//shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined

var a = [1,2,3,4,5];

var b = a.shift(); //a:[2,3,4,5] b:1

//unshift:将参数添加到原数组开头,并返回数组的长度

var a = [1,2,3,4,5];

var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5] b:7

//注:在IE6.0下测试返回值总为undefined,FF2.0下测试返回值为7,所以这个方法的返回值不可靠,需要用返回值时可用splice代替本方法来使用。

//pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined

var a = [1,2,3,4,5];

var b = a.pop(); //a:[1,2,3,4] b:5

//push:将参数添加到原数组末尾,并返回数组的长度

var a = [1,2,3,4,5];

var b = a.push(6,7); //a:[1,2,3,4,5,6,7] b:7

//concat:返回一个新数组,是将参数添加到原数组中构成的

var a = [1,2,3,4,5];

var b = a.concat(6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]

//splice(start,deleteCount,val1,val2,):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,

var a = [1,2,3,4,5];

var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]

var b = a.splice(0,1); //同shift

a.splice(0,0,-2,-1); var b = a.length; //同unshift

var b = a.splice(a.length-1,1); //同pop

a.splice(a.length,0,6,7); var b = a.length; //同push

//reverse:将数组反序

var a = [1,2,3,4,5];

var b = a.reverse(); //a:[5,4,3,2,1] b:[5,4,3,2,1]

//sort(orderfunction):按指定的参数对数组进行排序

var a = [1,2,3,4,5];

var b = a.sort(); //a:[1,2,3,4,5] b:[1,2,3,4,5]

//slice(start,end):返回从原数组中指定开始下标到结束下标之间的项组成的新数组

var a = [1,2,3,4,5];

var b = a.slice(2,5); //a:[1,2,3,4,5] b:[3,4,5]

//join(separator):将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符

var a = [1,2,3,4,5];

var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"

【字符串操作之】将一个字符串拆分成数组→→split方法

String中的split方法将一个字符串拆分成数组。

1、例如,使用空字符可以讲字符串每个分开。

var str:String="abcdefgh";
var arr:Array=str.split("");
trace(arr);
//a,b,c,d,e,f,g,h

2、使用特定符号分割。

var str:String="a_b_c_d";
var arr:Array=str.split("_");
trace(arr);
//a,b,c,d

3、splite还有第二个参数,用来限制放入数组内元素的个数。

var str:String="a_b_c_d_e";
var arr:Array=str.split("_",3);
trace(arr);
//a,b,c