浅谈javascript和java中的数组
作者:网络转载 发布时间:[ 2015/5/12 13:35:05 ] 推荐标签:开发语言
javascript中的数组
数组的创建
直接创建方式 var str = ['java', 'js'];
使用new创建方式: var a = new Array(10); // 定义长度为10的数组(可变)
另类new创建方式:var a = new Array(1, 2, 3, 4, 5); var b = [1, 2, 3, 4, 5];
二维数组(多维)创建方式:var a = new Array([1,2,3], [4,5,6], [7,8,9]); var b = [[1,2,3], [4,5,6], [7,8,9]];
创建数组注意事项:js中的数组长度、类型都是“动态性”的。即长度可变、类型多样。
数组的判别
面试的时候经常会被问道:js中怎么判断一个变量是数组类型?
答案有很多,这里我列举一下我所知道的一些判断方法
1 //千万别犯这种错误
2 console.log(typeof []) //object
3 // 方法一
4 var arr = [1,2,3];
5 console.log(arr.constructor === Array)//true
6 //方法二
7 console.log(arr instanceof Array) //true
8 //方法三
9 console.log(Object.prototype.toString.call(arr) === '[object Array]')//true
10 //方法四
11 Array.isArray(arr) //es5 新方法
数组的常用方法
arr. push();// 将一个或多个新元素添加到数组结尾,并返回数组新长度
arr.unshift();// 将一个或多个新元素添加到数组开始,返回数组新长度
arr.splice(arg1,arg2,arg3); //增删改都可以,非常强大
arr.pop(); //移除后一个元素并返回该元素值
arr.shift(); //移除前一个元素并返回该元素值,数组中元素自动前移
arr.slice(start, [end]); //以数组的形式返回数组的一部分,注意不包括 end 对应的元素
arrayObj.reverse(); //反转元素(前的排到后、后的排到前)
arrayObj.sort(); //对数组元素排序,返回数组地址
es5 新增的数组方法,详情见下面代码
1 //代码来自网络,大致能表达我的意思
2 // 定义一个数组,存储不同数据类型的元素
3 var array = [8, 2, 1, 5],
4 array2 = [
5 [0, 1],
6 [1, 2],
7 [2, 3]
8 ],
9 value;
10
11 /// Array.prototype.reduce = function(callback,initialValue) {};
12 // 化解数组:
13 // 调用reduce 方法提供的回调函数,
14 // 总共调用 数组的lenght - 1次 回调函数
15 // 第一次时 previousValue 为 initialValue
16 // 此后 reduce方法的返回值 作为 previousValue
17
18 // reduceRight 则从数组右边开始,进行上述操作
19
20 // 数组中元素累加
21 value = array.reduce(function (previousValue, currentValue, index, array) {
22 return previousValue + currentValue;
23 }
24 );
25 console.log(value); // 16
26
27 // 数组扁平化
28 value = array2.reduce(function (previousValue, currentValue, index, array) {
29 return previousValue.concat(currentValue);
30 }
31 );
32 console.log(value); // [ 0, 1, 1, 2, 2, 3 ]
33
34 /// Array.prototype.indexOf = function(searchElement,fromIndex) {};
35 // 从fromIndex索引处 向后寻找searchElement元素 找到并返回其索引,否则返回-1
36 // fromIndex 默认为 0
37 console.log(array.indexOf(1)); // 2
38 console.log(array.indexOf(3)); // -1
39
40 /// Array.prototype.lastIndexOf = function(searchElement,fromIndex) {};
41 // 从fromIndex索引处 向前寻找searchElement元素 找到并返回其索引,否则返回-1
42 console.log(array.lastIndexOf(1)); // 2
43 console.log(array.lastIndexOf(3)); // -1
44
45 /// Array.prototype.every = function(callback,thisObject) {};
46 // 测试数组的所有元素是否都通过了指定函数的测试
47 value = array.every(function (element, index, array) {
48 return element > 1;
49 });
50 console.log(value); // false
51
52
53 /// Array.prototype.filter = function(callback,thisObject) {};
54 // 数组过滤:
55 // 返回通过函数测试的 元素(当回调函数的返回true,表明元素通过测试 ),生成新的数组
56 value = array.filter(function (element, index, array) {
57 return element > 1;
58 });
59 console.log(value);// [ 8, 2, 5 ]
60
61
62 /// Array.prototype.forEach = function(callback,thisObject) {};
63 // 为每个数组元素执行一次回调函数。
64 array.forEach(function (element, index, array) {
65 console.log(element);
66 });
67
68
69 /// Array.prototype.map = function(callback,thisObject) {};
70 // 数组映射:
71 // 返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。
72 value = array.map(function (element, index, array) {
73 return element * element;
74 });
75 console.log(value); // [ 64, 4, 1, 25 ]
76
77
78 /// Array.prototype.some = function(callback,thisObject) {};
79 // 测试数组中的某些元素是否通过了指定函数的测试。
80 value = array.some(function (element, index, array) {
81 return element > 1;
82 });
83 console.log(value); // true
84
85
86 /// Array.prototype.isArray = function(object) {};
87 // 判断元素是否为数组类型
88 console.log(Array.isArray(array)); // true

sales@spasvo.com