数组api复习
(1)->判断是否是数组:Array.isArray()
var array = ["a","b","c","d","e","f"];
var temp = Array.isArray(array);
console.log(temp);//true
(2)->将数组转化为字符串:array.toString()
var array = ["a","b","c","d","e","f"];
var str = array.toString();
console.log( typeof str);//string
console.log(str);//a,b,c,d,e,f
(3)->valueOf的用法:给数组则返回打印该数组
var array2 = array.valueOf();
console.log(array2);
//["a","b","c","d","e","f"]
array[0]="123";
console.log(array);
//['123', 'b', 'c', 'd', 'e', 'f']
valueOf还有时间计算:
var a = date.valueOf();//返回时间间隔的毫秒值
(4)->array.pop()
// 取出数组最后一个值并修改了原数组
// 返回的值就是取出的值
var array = ["a","b","c","d","e","f"];
var temp = array.pop();
console.log(temp);
//f
console.log(array);
//["a","b","c","d","e"]
(5)->array.shift()
// 取出数组第一个值并修改了原数组
// 返回的值就是取出的值
var array = ["a","b","c","d","e","f"];
var temp = array.shift();
console.log(temp);
//a
console.log(array);
//["b","c","d","e","f"]
(6)->array.push()==>将字符添加到数组的尾部
var array = ["a","b","c","d","e"];
array.push("wodiu");
console.log(array);
//['a', 'b', 'c', 'd', 'e', 'wodiu']
(7)->array.unshift()==>将字符添加到数组的首部
var array = ["a","b","c","d","e"];
array.unshift("new");
console.log(array);
// ['new', 'a', 'b', 'c', 'd', 'e']
(8)->array.reverse()==>返回翻转后的数组==>原数组会被修改
var array = ["a","b","c","d","e"];
var temp = array.reverse();
array[0] = "0";
console.log(temp);
//['0', 'd', 'c', 'b', 'a']
console.log(array);
//['0', 'd', 'c', 'b', 'a']
(9)->array.concat(数组)==>返回新数组,原数组不变
var array = ["a","b","c","d","e"];
var array3 = [1,2,3];
var temp = array.concat(array3);
console.log(temp);
//['a', 'b', 'c', 'd', 'e', 1, 2, 3]
console.log(array);
//['a', 'b', 'c', 'd', 'e']
console.log(array3);
//[1, 2, 3]
(10)->array.slice(开始位置,结束位置)==>
// 原数组不会被修改
// 索引值包左不包右
var array = ["a","b","c","d","e"];
var temp = array.slice(2,4);
console.log(temp);
//['c', 'd']
console.log(array);
//['a', 'b', 'c', 'd', 'e']
(11)->array.splice(开始位置,结束位置)==>
// 原数组会被修改
// 索引值全包
// 索引的元素会被剔除
var array = ["a","b","c","d","e"];
var temp = array.splice(1,3);
console.log(temp);
//['b', 'c', 'd']
console.log(array);
//['a', 'e']
(12)->数组元素的查找
array.indexOf("c")==>从前往后查找第一个c的位置==>返回索引值
// 从下标为0的位置开始找,找到第一个返回下标
var array = ["a","b","c","d","e"];
var temp = array.indexOf("c")
console.log(temp);
//2
array.lastIndexOf("e")==>从后往前查找第一个e的位置==>返回索引值
var array = ["a","b","c","d","e"];
var temp = array.lastIndexOf("e");
console.log(temp);
//4
(13)array.join("")==>将数组改为字符串,且中间无间隔
var array = ["a","b","c","d","e"];
var temp = array.join("");
console.log(temp);
//abcde
(14)array.sort(function(a,b){})==>数组排序
var array = [1,3,2,19,5,7,8,9];
var temp = array.sort(function(a,b){
return a-b;
//从小到大排序
})
console.log(temp);
//[1, 2, 3, 5, 7, 8, 9, 19]
console.log(array);
//[1, 2, 3, 5, 7, 8, 9, 19]