Array 对象用于在单个的变量中存储多个值。
1. 创建数组
var array = new Array();
var array = [];
数组中的元素类型是可以不同的
arr.length数组的长度
2. 获取数组元素
arr[0];
3. 数组常用方法
1. arr.indexOf(searchElement,fromIndex=0)
indexOf(searchValue[, fromIndex]) searchValue:需检索的字符串值 fromIndex:字符串中开始检索的位置
indexOf() 方法返回指定元素在数组(或字符串)中首次出现的位置,未找到则返回-1
LastIndexOf() 与indexOf()同理,只是从末尾开始向前检索,.
var str = 'wow, hello world!';
var a5 = ['tt', 'uu', 'yy', 'uu', 'ii'];
str.indexOf('wo', 3); // 11
a5.indexOf('uu'); // 2
a5.indexOf('uu', 2); // 3
str.lastIndexOf('o'); // 7
2. forEach(callback[,thisArg])
像map()一样对每一个元素进行操作,但与map()不同,forEach()内的函数提供3个参数(elem, index, array)(并且本身没有返回值,会修改原始数组! 无法使用 return false 或 break 退出循环)
var arr = [4, 44, 444],
sum = 0;
arr.forEach(function (elem, i, a) { // a[i] === v
a[i] = elem + 1;
sum += a[i];
});
console.log(arr); // [5, 45, 445]. 修改原始数组,而不返回新数组
console.log(sum); // 495
*关于Map
map() 对每一个元素进行操作,并返回一个新的数组(不修改原始数组)
var arr = [1, 2, 3, 4, 5];
var square = arr.map(function (elem) {
return elem * elem;
});
console.log(arr); // [1, 2, 3, 4, 5]. 不修改原始数组,只返回新数组
console.log(square); // [1, 4, 9, 16, 25]
3. filter() 返回符合条件的元素组成的数组。
var a4 = [5, "sdf", true, "ooo", { name: "as" }];
var filter = a4.filter(function (elem) {
return typeof elem === "string";
});
console.log(filter); // ["sdf", "ooo"]
4. every & some
every() 用于判断———对数组中的每个元素运行一次回调函数,全真则真,一假则假。
some() 用于判断———对数组的每个元素运行一次回调函数,全假则假,一真则真。
var a4 = [5, "sdf", true, "ooo", { name: "as" }];
var every = [5, 6, 7, 8].every(function (elem) {
return elem >= 6;
});
console.log(every); // false
var some = [5, 6, 7, false].some(function (elem) {
return typeof elem === "boolean";
});
console.log(some); // true
[].every(function (el) { return el; }) // true. 注意当数组没为空的时候 every 返回 true
5. reduce() & reduceRigth()
按一定规则化简数组元素,将前后两个元素化简成一个值,并返回该值。回调函数的返回值为累积结果(previousValue),并且此返回值在下一次调用该回调函数时作为参数提供
reduceRight() 从右到左进行化简
var a6 = [1, 10, 100, 1000];
var reduce1 = a6.reduce(function (previousValue, currentValue) { // 数组求和
return previousValue + currentValue;
});
console.log(reduce1); // 1111
var reduce2 = a6.reduce(function (previousValue, currentValue) { // 数组求积
return previousValue * currentValue;
});
console.log(reduce2); // 1000000
var reduce3 = a6.reduce(function (previousValue, currentValue) { // 数组求最大值
return (currentValue > previousValue) ? currentValue : previousValue;
});
console.log(reduce3); // 1000
6. reverse()
方法用于颠倒数组中元素的顺序,返回逆序数组
[1, 2, 3, 4].reverse(); // [4, 3, 2, 1]
7. sort()
方法用于对数组的元素进行排序 (会直接对数组进行修改! 返回排序后的数组) (如果没有使用参数则按照字符编码的顺序进行排序)
var arr3 = ["Allen", "Thomas", "James", "Adrew"];
var arr4 = [33, 222, 4, 1111];
arr3.sort(); // ["Adrew", "Allen", "James", "Thomas"]. sorted alphabetically
arr4.sort(); // [1111, 222, 33, 4]. sorted alphabetically
console.log(arr4); // [1111, 222, 33, 4]. (arr4已经被修改)
[23, 9, 4, 78, 3].sort(function (a, b) { // 回调函数如果返回正值则交换两个参数在数组中的位置,否则不变
return a - b; // [3, 4, 9, 23, 78]. 对于数字排序,应该使用 'a-b'(升序)或 'b-a'(降序)
});
["abd","cba","ba"].sort(function (a, b) { // 对于字母排序,应该使用 '>'(顺序)或 '<'(倒序)
return a.slice(1) > b.slice(1); // 除去第一个字母后按照字母表顺序排列,返回["ba","cba","abd"]
});
8. push() & unshift()
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度
var arr10 = [];
for (var i = 0; i < 4; i++) {
var j = Math.floor(Math.random() * 100); // randomly generate 4 integars from 0 to 100
arr10.push(j, "A"); // push() can add one or more elements to the end of the array
}
console.log(arr10); // [10, "A", 23, "A", 72, "A", 61, "A"]
var length = arr10.unshift("4444", 4444); // 返回新的长度,length 为 10
console.log(arr10); // ["4444", 4444, 10, "A", 23, "A", 72, "A", 61, "A"]
9. pop() & shift()
pop() 方法用于删除并返回数组的最后一个元素
shift() 方法用于删除并返回数组的第一个元素
var arr11 = ["W", "X", "Y", "Z"];
console.log(arr11.pop()); // "Z"
console.log(arr11); // ["W", "X", "Y"]
console.log(arr11.shift()); // "W"
console.log(arr11); // ["X", "Y"]
10. splice()
splice() 方法用于插入、删除或替换数组元素(会直接对数组进行修改! 如果删除了元素,则返回被删除元素的数组,否则返回空数组)
splice(index,howMany,element1,.....,elementX)
-- index: 必需。规定从何处添加/删除元素
-- howMany: 删除元素的个数 (如果未指定此参数,则删除从 index 开始到原数组结尾的所有元素,相当于.slice(index))
-- element1,.....,elementX: 要添加到数组的新元素。从 index 所指的下标处开始插入
var arr8 = ["a", "b", "c", "d", "e"];
var arr9 = ["a", "b", "c", "d", "e"];
console.log(arr8.splice(1, 1)); // ["b"]. 返回删除的元素
console.log(arr8); // ["a", "c", "d", "e"]. 原数组已被修改
console.log(arr8.splice(2, 0, "1947", 7788)); // []. 未删除元素返回空数组
console.log(arr8); // ["a", "c", "1947", 7788, "d", "e"]
* reverse, sort, push, unshift, shift, pop, splice都改变了原来的数组
11. slice()
arr.slice(start,end)截取的数组(或字符串)包含start,而不含end
slice()方法可提取数组(或字符串)的某个部分,返回被提取的部分,默认参数是0。而substring()只能用于字符串截取
var arr7 = [1, 2, 3, 4, 5, 6];
arr7.slice(1, 2); // [2]
arr7.slice(1, -1); // [2, 3, 4, 5]
arr7.slice(3); // [4, 5, 6]
var another = [1, 2, 3].slice(0); // [1, 2, 3]. 截取所有元素,用于数组浅拷贝. 相当于 arr.slice()
12. concat()
创建并返回一个新数组,用于连接两个或多个数组,相当于字符串连接中的"+"
var arr6 = arr3.concat(arr4);
console.log(arr6); // ["Adrew", "Allen", "James", "Thomas", 1111, 222, 33, 4]
var arr7 = [1, 2].concat([3, 4], ["Z", "Z"]);
console.log(arr7); // [1, 2, 3, 4, "Z", "Z"]
* concat vs push
1. comcat 与 push 的区别: 前者创建并返回一个新数组,而后者修改原始数组
2. 将一个数组插入另一个数组的指定位置的操作
13. join()
把数组中的所有元素放入一个字符串,通过指定的分隔符进行分隔 (数组与字符串之间的桥梁函数,另一个是split())
var arr1 = ["!!!", "&&&", "$$$", "%%%"];
arr1.join(); // "!!!,&&&,$$$,%%%". 相当于 arr1.join(',');
arr1.join(" | "); // "!!! | &&& | $$$ | %%%"
14. toString()
toString() 用于把Array、Boolean、Date、Error、Function、Number等对象转换为字符串
toLocaleString() 把数组转换为本地字符串
["aaa", "bbb", "ccc", "ddd"].toString(); // "aaa,bbb,ccc,ddd"
(34).toString(2); // "100010"(参数指定的基数或底数,底数范围为2-36)
(new Date()).toString(); // "Wed Apr 16 2014 23:23:07 GMT+0800 (中国标准时间)"
(new Date()).toLocaleString(); // "2014年5月28日 下午3:41:34"
* slice, concat, join, map, reduce对原来的数组没有修改