JS基础知识点-数组
数组对象
JavaScript中没有明确的数组数据类型。但是,我们可以通过使用内置Array对象和它的方法对数组进行操作。Array对象有很多操作数组的方法,比如合并、反转、排序等。数组对象有一个决定数组长度和使用正则表达式操作其他属性的属性。
数组方法
添加删除
unshift() 在数组开头添加一个或多个元素,并返回数组的新长度。
var myArray = new Array ("1", "2", "3");
myArray.unshift("4", "5");
// myArray becomes ["4", "5", "1", "2", "3"]
push() 在数组末尾添加一个或多个元素,并返回数组操作后的长度。
var myArray = new Array("1", "2");
myArray.push("3");
// myArray is now ["1", "2", "3"]
shift() 从数组移出第一个元素,并返回该元素。
var myArray = new Array ("1", "2", "3");
var first = myArray.shift();
// myArray is now ["2", "3"], first is "1"
pop() 从数组移出最后一个元素,并返回该元素。
var myArray = new Array("1", "2", "3");
var last = myArray.pop();
// myArray is now ["1", "2"], last = "3"
合并数组
concat() 连接两个数组并返回一个新的数组。
var myArray = new Array("1", "2", "3");
myArray = myArray.concat("a", "b", "c");
// myArray is now ["1", "2", "3", "a", "b", "c"]
数组转换类型
join(deliminator = ‘,’) 将数组的所有元素连接成一个字符串,用指定分隔符分割。
var myArray = new Array("Wind", "Rain", "Fire");
var list = myArray.join(" - "); // list is "Wind - Rain - Fire"
toString() 转换成成一个字符串。
var myArray = new Array("Wind", "Rain", "Fire");
var list = myArray.toString(); // list is "'Wind,Rain,Fire"
toLocaleString() 转换成本地格式字符串。
var myArray = new Array("Wind", "Rain", "Fire");
var list = myArray.toLocaleString(); // list is "'Wind,Rain,Fire"
提取子数组
slice(start_index, upto_index) 从数组提取一个片段,并作为一个新数组返回。
var myArray = new Array ("a", "b", "c", "d", "e");
myArray = myArray.slice(1, 4); // starts at index 1 and extracts all elements
// until index 3, returning [ "b", "c", "d"]
增删改
splice(index, count_to_remove, addElement1, addElement2, …)从数组移出一些元素,(可选)并替换它们。
var myArray = new Array ("1", "2", "3", "4", "5");
myArray.splice(1, 3, "a", "b", "c", "d");
// myArray is now ["1", "a", "b", "c", "d", "5"]
// This code started at index one (or where the "2" was),
// removed 3 elements there, and then inserted all consecutive
// elements in its place.
排序
reverse() 颠倒数组元素的顺序:第一个变成最后一个,最后一个变成第一个。
var myArray = new Array ("1", "2", "3");
myArray.reverse();
// transposes the array so that myArray = [ "3", "2", "1" ]
sort() 给数组元素排序。
var sortFn = function(a, b){
if (a[a.length - 1] < b[b.length - 1]) return -1;
if (a[a.length - 1] > b[b.length - 1]) return 1;
if (a[a.length - 1] == b[b.length - 1]) return 0;
}
myArray.sort(sortFn);
// sorts the array so that myArray = ["Wind","Fire","Rain"]
位置查找
indexOf(searchElement[, fromIndex]) 在数组中搜索searchElement 并返回第一个匹配的索引。
var a = ['a', 'b', 'a', 'b', 'a'];
console.log(a.indexOf('b')); // logs 1
// Now try again, starting from after the last match
console.log(a.indexOf('b', 2)); // logs 3
console.log(a.indexOf('z')); // logs -1, because 'z' was not found
lastIndexOf(searchElement[, fromIndex]) 和 indexOf 差不多,但这是从结尾开始,并且是反向搜索。
var a = ['a', 'b', 'c', 'd', 'a', 'b'];
console.log(a.lastIndexOf('b')); // logs 5
// Now try again, starting from before the last match
console.log(a.lastIndexOf('b', 4)); // logs 1
console.log(a.lastIndexOf('z')); // logs -1
迭代方法
forEach(callback[, thisObject]) 在数组每个元素项上执行callback。
var a = ['a', 'b', 'c'];
a.forEach(function(element) { console.log(element);} );
// logs each item in turn
map(callback[, thisObject]) 在数组的每个单元项上执行callback函数,并返回包含回调函数返回值的新数组。
var a1 = ['a', 'b', 'c'];
var a2 = a1.map(function(item) { return item.toUpperCase(); });
console.log(a2); // logs A,B,C
filter(callback[, thisObject]) 返回一个包含所有在回调函数上返回为true的元素的新数组。
var a1 = ['a', 10, 'b', 20, 'c', 30];
var a2 = a1.filter(function(item) { return typeof item == 'number'; });
console.log(a2); // logs 10,20,30
every(callback[, thisObject]) 当数组中每一个元素在callback上被返回true时就返回true。
function isNumber(value){
return typeof value == 'number';
}
var a1 = [1, 2, 3];
console.log(a1.every(isNumber)); // logs true
var a2 = [1, '2', 3];
console.log(a2.every(isNumber)); // logs false
some(callback[, thisObject]) 只要数组中有一项在callback上被返回true,就返回true。
function isNumber(value){
return typeof value == 'number';
}
var a1 = [1, 2, 3];
console.log(a1.some(isNumber)); // logs true
var a2 = [1, '2', 3];
console.log(a2.some(isNumber)); // logs true
var a3 = ['1', '2', '3'];
console.log(a3.some(isNumber)); // logs false
缩小方法
reduce(callback[, initialValue]) 使用回调函数 callback(firstValue, secondValue) 把数组列表计算成一个单一值(译者注:他数组元素两两递归处理的方式把数组计算成一个值)
var a = [10, 20, 30];
var total = a.reduce(function(first, second) { return first + second; }, 0);
console.log(total) // Prints 60
reduceRight(callback[, initalvalue]) 和 reduce()相似,但这从最后一个元素开始的。