javascript中Array.protototype方法总结

本文详细介绍了 JavaScript 中 Array 对象的多种方法,包括查找、增删、连接等操作,并通过实例展示了如何高效地利用这些方法处理数组数据。

javascript 中Array对象是我们页面技术中大量使用的对象,充分掌握该对象的使用对我们前端页面编程会有很大的帮助,可以避免我们在处理数据过程中的一些不必要的麻烦。由于对Array原型方法的不了解,可能致使我们花费大量的时间再数据的处理上,而当你对Array有充分的了解后,原来的操作可能只需简单几步便可达成目标。

1.Array.prototype.indexOf([Object] searchElement,[Number,optional] fromIndex)

找出数组中参数指定的searchElement元素的索引,从前往后开始查找,返回第一个匹配元素的索引。可选参数fromIndex指定从哪个索引位置开始查找。

2.Array.prototype.lastIndexOf([Object] searchElement,[Number,optional] fromIndex)

和indexOf方法一样,查找匹配元素的索引,只是该方法方向为从数组尾部开始查找,fromIndex指定开始查找的索引,返回匹配元素的索引。

3.Array.prototype.push([Array,optional] items)

将参数指定的内容push到现有数组中,在数组的尾部追加。

var arr = ["a"];
arr.push("b");
console.log(arr);
output:

["a","b"]

4.Array.prototype.pop()

从数组尾部删除一个元素,返回被删除的元素。

5.Array.prototype.unshift([Array,optional] items)

从数组的头部增加一个指定元素,返回增加后数组的Length值。

6.Array.prototype.shift()

从数组的头部删除一个元素,返回被删除的元素的。

7.Array.prototype.concat([Array,optional] items)

将数组与参数中指定的多个数组链接起来,返回一个新的数组引用,原来的数组不做改变,且如果参数数组中包含子数组,则把子数组当做一个元素对待。

var arr_b = arr.concat(['c','d',["e",'f',["g","h"]]],["i","j"]);
console.log(arr_b);
output:

["a", "b", "c", "d", Array[3], "i", "j"]

8.Array.prototype.join([String,optional] seperator)

使用参数指定的连字符将数组元素连接起来,返回连接后的字符串。

var arr = ["a","b"];
var str = arr.join(","); 
console.log(str);
output:

a,b

9.Array.prototype.slice([Number,optional] start,[Number,optional] end)

按照参数指定的起始和结束位置对原数组进行切割,返回切割的结果数组。如果给定的位置参数不对,返回空数组。

var arr = ["a","b","c","d","e","f","g"];
var sub = arr.slice(2,-2);
console.log(sub);
oupput:

["c","d","e"]

10.Array.prototype.splice([Number,optional] start,[Number,optional] deleteCount,[Array,optional] items)

删除/增加数组元素,在参数start指定的索引处删除指定数目(deleteCount)的数组元素并将参数items指定的元素加入到删除的索引位置。返回被删除的数组元素组成的数组。

var arr = ["a","b","c","d","e","f","g"];
var sub = arr.splice(2,1,"x","y");
console.log(sub);
console.log(arr);
output:

["c"] 

["a","b","x","y","d", "e", "f", "g"]

11.Array.prototype.map([Function] callback,[Object,optional] thisObject)

对数组中的每个元素都执行一次指定的函数(callback),并且以每次返回的结果为元素创建一个新数组。它只对数组中的非空元素执行指定的函数,没有赋值或者已经删除的元素将被忽略。

回调函数可以有三个参数:当前元素,当前元素的索引和当前的数组对象。

如参数 thisObject 被传递进来,它将被当做回调函数(callback)内部的 this 对象,如果没有传递或者为null,那么将会使用全局对象。

var arr = ["a","b","c","d","e","f","g"];
var map_arr = arr.map(function(v,i,a){
      return v+i;
        });
console.log(map_arr);
output:

["a0","b1","c2", "d3", "e4", "f5", "g6"]

12.Array.prototype.filter([Function] callback,[Object,optional] thisObject)

通过将数组元素传递至回调函数判断是否过滤此数组元素,返回true表示保留,否则过滤。返回过滤后的数组。原数组保持不变。参数规则和map方法相同。

var arr = ["a","b","c","d","e","f","g",1,2,3];
        var arr_filter = arr.filter(function(v,i,a){
            if(typeof v == 'string')
                return true;
            else return false;
        });
        console.log(arr);
        console.log(arr_filter);
output:

["a","b","c", "d", "e", "f", "g", 1,2, 3] 

["a","b","c", "d", "e", "f", "g"]

13.Array.prototype.sort([Function,optional] compareFn)

根据参数提供的回调函数对数组元素进行排序,回调函数接收两个参数,分别为待排序的数组元素,根据返回值确定元素的顺序,如果为true,则a排在b之后。

 var arr = ["a","b","c","d","e","f","g",1,2,3];
        var arr_sort= arr.sort(function(a,b){
            if(a<b)
             return true;
            else return false;
        });
        console.log(arr);
output:

["g","f","e", "d", "c", "b", "a", 3,2, 1]

14.Array.prototype.reverse()

将原数组全部元素颠倒过来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值