/*
:
typeof :typeof [] object
typeof {} object
1. var obj={};var obj=[];var obj=function(){};
obj instanceof / (Array,Function,...)
2. Array.prototype.isPrototypeof(obj)
*3. Object.prototype.toString.call(obj) [object Array]
4. ES5: Array.isArray(obj)
obj , true, false!
isArray :
*/
// Array isArray ,
if(!Array.isArray){
// Array isArray , obj
Array.isArray=function(obj){
// var result= obj toString
var result=
Object.prototype.toString.call(obj);
//"[object XXXXX]"
// result "Array"
if(result.indexOf("Array")!=-1){//
return true;// true
}else{//
return false;// false
}
}
}
document.write(Array.isArray({})+"
");//false
document.write(Array.isArray([])+"
");//true
// API:
//every:
var arr1=[1,2,3,4,5];
var arr2=[2,4,6,8,10];
console.log(
arr1.every(function(value,index,array){
return value%2==0;
})
);//false
console.log(
arr2.every(function(value,index,array){
return value%2==0;
})
);//true
/* some: * *
true
false*/
console.log(
arr1.some(function(value,index,array){
return value%2==0;
})
);//true
console.log(
arr2.some(function(value,index,array){
return value%2!=0;
})
);//false
//forEach:
var arr=[1,2,3,4,5];
console.log(String(arr));//1,2,3,4,5
arr.forEach(function(value,index,array){
array[index]*=2;
});
/**map: , , * *
var newArr=arr.map(function(value,index,array){
return
})*/
var newArr=arr.map(function(value,index,array){
return value*2;
});
console.log(String(arr));//2,4,6,8,10
console.log(String(newArr));//4,8,12,16,20
//filter: ,
var subArr=arr.filter(function(value,index,array){
return value%2==0;
});
console.log(String(subArr));//2,4,6,8,10
//reduce:
var result=arr.reduce(
function(prev,value,index,array){
return prev+value;
},
100
);
console.log(result);//130