数组API
- API: Application Programming Interface,应用程序编程接口;
- js中对象提供的方法就叫做API;
instanceof
检测一个对象是否是数组;(用来对付复杂数据类型;)
A instanceof B
例:
var arr = [1,2,3];
console.log(arr instanceof Array);
Array.isArray( )
Array.isArray(参数);
例:
var arr = [1,2,3];
var num = 123;
console.log(Array.isArray(arr));
console.log(Array.isArray(num));
toString( )
数组.toString();
例:
var arr = ["aaa","bbb","ccc"];
console.log(arr.toString());
valueOf( )
数组.valueOf(); //返回数组本身;
例:
var arr = ["aaa","bbb","ccc"];
console.log(arr.valueOf()); //返回数组本身 ["aaa","bbb","ccc"]
数组.join(参数)
数组.join(参数);
console.log(arr.join());
console.log(arr.join("|"));
console.log(arr.join("&"));
console.log(arr.join(" "));
console.log(arr.join(""));
数组元素的添加和删除
push( )和pop( )
1. 数组.push()
2. 数组.pop()
例:
var arr = [1,2,3];
var aaa = arr.push("abc");
console.log(arr);
console.log(aaa);
aaa = arr.pop();
console.log(arr);
console.log(aaa);
unshift( )和shift( )
1. 数组.unshift()
2. 数组.shift()
例:
var arr = [1,2,3];
aaa = arr.unshift("abc");
console.log(arr);
console.log(aaa);
aaa = arr.shift();
console.log(arr);
console.log(aaa);
数组元素排序
reverse( )
reverse()
例:
var arr1 = [1,2,3,4,5];
var aaa = arr1.reverse();
sort( )
sort()
例:
var arr = [4,5,1,3,2,7,6];
var aaa =arr.sort();
console.log(aaa);
console.log(aaa === arr);
var arr2 = ["c","e","d","a","b"];
var bbb = arr2.sort();
console.log(bbb);
console.log(bbb===arr2);
sort()
例:
var arr = [4,5,1,13,2,7,6];
arr.sort(function (a,b) {
return a-b;
});
console.log(arr);
sort( )底层原理
var aaa = bubbleSort([1,12,3], function (a,b) {
return b-a;
});
console.log(aaa);
function bubbleSort(array,fn){
for(var i=0;i<array.length-1;i++){
for(var j=0;j<array.length-1-i;j++){
if(fn(array[j],array[j+1])>0){
var temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
数组元素的操作
concat( )
数组1.concat(数组2); // 链接两个数组;
var arr1 = [1,2,3];
var arr2 = ["a","b","c"];
var arr3 = arr1.concat(arr2);
console.log(arr3) // [1, 2, 3, "a", "b", "c"]
slice( )
数组.slice(开始索引值,结束索引值); //数组截取;
例 :
var arr = [1, 2, 3, "a", "b", "c"];
console.log(arr.slice(3)); //从索引值为3截取到最后;["a", "b", "c"]
console.log(arr.slice(0,3)); //包左不包右;[1, 2, 3]
console.log(arr.slice(-2)); //负数是后几个;["b", "c"]
console.log(arr.slice(3,0)); //如果前面的比后面的大,那么就是[];[]
console.log(arr); //原数组不被修改;[1, 2, 3, "a", "b", "c"]
splice( )
数组.splice(开始索引值,删除几个,替换内容1,替换内容2,...); // 替换和删除;
//改变原数组;返回值是被删除/替换的内容
例:
var arr = [1,2,3,4,5,6,"a", "b", "c"]
arr.splice(5); //从索引值为3截取到最后;(删除)
console.log(arr); // [1, 2, 3, 4, 5]
arr.splice(1,2); //(删除指定个数)从索引为1的开始删除2个
console.log(arr); //[1, 4, 5]
//替换
var arr = [1,2,3,4,5,6,"a", "b", "c"];
console.log(arr.splice(3,3,"aaa","bbb","ccc")); //(删除指定数并替换)
console.log(arr); // [1, 2, 3, "aaa", "bbb", "ccc", "a", "b", "c"]
// 添加
arr.splice(3,0,"aaa","bbb","ccc");//(删除指定个数)
//
console.log(arr);//截取或者替换之后的; [1, 2, 3, "aaa", "bbb", "ccc", "aaa", "bbb", "ccc", "a", "b", "c"]
indexOf / lastIndexOf
数组.indexOf(元素);
数组.lastIndexOf(元素);
例:
var arr = ["a","b","c","d","c","b","b"];
console.log(arr.indexOf("b"));
console.log(arr.lastIndexOf("b"));
console.log(arr.indexOf("xxx"));
数组迭代(遍历)
every()
对数组中每一项运行回调函数,如果都返回true,every返回true,
如果有一项返回false,则停止遍历 every返回false;不写默认返回false
像保镖失误一次,游戏结束!!!
例:
1. var arr = [111,222,333,444,555];
arr.every(function (a,b,c) {
console.log(a);
console.log(b);
console.log(c);
console.log("-----");
return true;
});
2.
var bool = arr.every(function (element, index, array) {
if(element > 200){
return true;
}else{
return false;
}
})
alert(bool);
filter()
例:
var arr = [111,222,333,444,555];
var newArr = arr.filter(function (element, index, array) {
if(element%2 === 0){
return true;
}else{
return false;
}
})
console.log(newArr);
forEach()
例:
var arr = [111,222,333,444,555];
var sum = 0;
var aaa = arr.forEach(function (element,index,array) {
console.log(element);
console.log(index);
console.log(array);
sum += element;
});
console.log(sum);
console.log(aaa);
map()
例:
var arr = [111,222,333,444,555];
var newArr = arr.map(function (element, index, array) {
if(index == 2){
return element;
}
return element*100;
})
console.log(newArr);
some()
例:
var arr = [111,222,333,444,555];
var bool = arr.some(function (ele,i,array) {
if(ele%3 == 0){
return true;
}
return false;
})
alert(bool);
数组清空
1. arr.length = 0;
2. arr.splice(0);
3. arr = [];
例:
fn(111,222,333);
function fn(){
arguments.length = 0;
arguments.splice(0);
arguments = [];
console.log(arguments);
}
数组案例
1.将一个字符串数组输出为|分割的形式,比如“刘备|张飞|关羽”。使用两种方式实现
var arr = ["刘备","张飞","关羽"];
var separator = "|";
var str = arr[0];
for(var i=1;i<arr.length;i++){
str += separator+arr[i];
}
console.log(str);
console.log(arr.join("|"));
2.将一个字符串数组的元素的顺序进行反转。["a", "b", "c", "d"] -> [ "d","c","b","a"]。使用两种种方式实现。提示:第i个和第length-i-1个进行交换
var arr = ["a", "b", "c", "d"];
console.log(arr.reverse());
for(var i=0;i<arr.length/2;i++){
var temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
console.log(arr);
3.工资的数组[1500, 1200, 2000, 2100, 1800],把工资超过2000的删除
var arr = [1500, 1200, 2000, 2100, 1800];
var newArr = arr.filter(function (ele, i, array) {
if(ele<2000){
return true;
}else{
return false;
}
});
console.log(newArr);
4.["c", "a", "z", "a", "x", "a"]找到数组中每一个a出现的位置
var arr = ["c", "a", "z", "a", "x", "a"];
arr.forEach(function (ele, index, array) {
if("a" === ele){
console.log(index);
}
});
5.编写一个方法去掉一个数组的重复元素 (数组去重)
var arr = ["鸣人","鸣人","佐助","佐助","小樱","小樱"];
var newArr = [];
arr.forEach(function (ele,index,array) {
if(newArr.indexOf(ele) === -1){
newArr.push(ele);
}
});
console.log(newArr);
https://segmentfault.com/a/1190000012276002