ary.push(10);
//等效于 ary[ary.length] = 10;
//等效于 ary.splice(ary.length, 0, 10);
ary.pop();
//等效于 ary.length--;
//等效于 ary.length-=1;
//等效于 ary.length=ary.length-1;
//等效于 ary.splice(ary.length-1);
var ary = [12, 23, 34, 45];
var res = ary.toString();
console.log(res);//->"12,23,34,45"
var ary = [12, 23, 34, 45];
var res = ary.join("+");
console.log(res);//->"12+23+34+45"
var ary = [12,3,45,65,5,56,78];
var result = ary.reverse();
console.log(ary); //->[78, 56, 5, 65, 45, 3, 12]
console.log(result); //->[78, 56, 5, 65, 45, 3, 12]
ary.sort() 这样的话只能处理10以内的数字进行排序,因为它是按照UNICODE编码的值进行排序的
ary.sort(function (a, b) {return a - b;}); 由小到大排序
ary.sort(function (a, b) {return b - a;}); 由大到小排序
forEach:数组中有几项,我们的函数就执行几次;
item->当前遍历的数组中这一项的值 index->当前这一项的索引->原来的数组不变
ary.forEach(function (item, index) {
console.log(item, index);
});
var a=new Array(1,2,3,4,5,6);
console.log(a.filter(function(e){
return e%2==0;
})); // [2, 4, 6]
console.log(a); //[1, 2, 3, 4, 5, 6]
var ary = [12,34,230,54,34,25,26,14];
var newAry = ary.map(function (item, index) {
return item * 10;
});
console.log(newAry);//->[120, 340, 2300, 540, 340, 250, 260, 140]
var arr=[1,2,3,4,5,4,3,2,1];
var everyResult=arr.every(function(item,index,array){
return (item>2);
});
console.log(everyResult);//->false
var arr=[1,2,3,4,5,4,3,2,1];
var someResult=arr.some(function(item,index,array){
return (item>2);
});
console.log(someResult);//->true
var arr = ["apple","orange","apple","orange","pear","orange"];
function getWordCnt(){
return arr.reduce(function(prev,next){
prev[next] = (prev[next] + 1) || 1;
return prev;
},{});}
console.log(getWordCnt());//->{ "apple": 2, "orange": 3, "pear": 1 }
var a = new Array(123);
var b = new Date();
console.log(Array.isArray(a)); //->true
console.log(Array.isArray(b)); //->false
console.log(typeof 12); //"number"
var num = "abc";
console.log(typeof num); //"string"
console.log(typeof typeof typeof function(){}); //"string"
var ary = [12,34,230,54,34,25,26,14],exp = /^\d$/;
console.log(typeof ary); //"object"
console.log(typeof exp); //"object"
var obj = [12, 23];
console.log(obj instanceof Array); //->true
console.log(obj instanceof RegExp); //->false
console.log(1 instanceof Number);//->false
console.log(new Number(1) instanceof Number);//->true
var ary = [];
console.log(ary instanceof Array);//->true
console.log(ary instanceof Object);//->true
var obj = [];
console.log(obj.constructor === Array);//->true
console.log(obj.constructor === RegExp);//->false
var num=1;
console.log(num.constructor===Number);
var reg = /^\d$/;
console.log(reg.constructor === RegExp);//->true
console.log(reg.constructor === Object);//->false
function Fn() {}
Fn.prototype = new Array;
var f = new Fn;
console.log(f.constructor);//->Array
console.log(Object.prototype.toString.call([]));//->"[object Array]"
console.log(Object.prototype.toString.call(/^$/));//->"[object RegExp]"
console.log(({}).toString.call(new Date));//->"[object Date]"
console.log(Object.prototype.toString.call(1));//->"[object Number]"
console.log(Object.prototype.toString.call("abc"));//->"[object String]"
console.log(Object.prototype.toString.call(true));//->"[object Boolean]"
console.log(Object.prototype.toString.call(null));//->"[object Null]"
console.log(Object.prototype.toString.call(undefined));//->"[object Undefined]"
console.log(Object.prototype.toString.call(function () {
}));//->"[object Function]"function Fn() {}
Fn.prototype = new Array;
var f = new Fn;
console.log(f instanceof Array);//->true
console.log(Object.prototype.toString.call(f) === "[object Array]");//->false
console.log((128).toString(2/8/10)); ->把数字转换为二进制/八进制/十进制