<script type="text/javascript">
//every() 方法测试数组的所有元素是否都通过了指定函数的测试
var arr =[1,2,3,4,5,6,7,8];
var result = arr.every(function(item,index){if(item<9){
return true;}})
console.log(result);
//参数1:callback
/*为数组中每个元素执行的函数,该函数接收三个参数:
currentValue(当前值):数组中正在处理的当前元素。
index(索引):数组中正在处理的当前元素的索引。
array:forEach()方法正在操作的数组。*/
//参数2:thisArg
/*可选参数。当执行回调 函数时用作this的值。*/
//返回值:true false
//是否影响老数组:不影响
</script>
some() 方法
<script type="text/javascript">
//some() 方法测试数组中的某些元素是否通过由提供的函数实现的测试。
var arr =[1,2,3,4,5,6,7,8];
var result = arr.some(function(item,index){if(item > 7){
return true;}})
console.log(result);
//参数1:callback
/*为数组中每个元素执行的函数,该函数接收三个参数:
currentValue(当前值):数组中正在处理的当前元素。
index(索引):数组中正在处理的当前元素的索引。
array:forEach()方法正在操作的数组。*/
//参数2:thisArg
/*可选参数。当执行回调 函数时用作this的值。*///返回值:true false
//是否影响老数组:不影响
//Polyfill:衬垫代码
if (!Array.prototype.some){
Array.prototype.some = function(fun /*, thisArg */){'use strict';
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function')
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++){
if (i in t && fun.call(thisArg, t[i], i, t))
return true;}
return false;};}
</script>
filter() 方法
<script type="text/javascript">
//filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
var arr =[1,2,3,4,5,6,7,8];
var result = arr.filter(function(item,index){if(item > 7){
return true;}})
console.log(result);
//参数1:callback
/*为数组中每个元素执行的函数,该函数接收三个参数:
currentValue(当前值):数组中正在处理的当前元素。
index(索引):数组中正在处理的当前元素的索引。
array:forEach()方法正在操作的数组。*/
//参数2:thisArg
/*可选参数。当执行回调 函数时用作this的值。*///返回值: 一个新的通过测试的元素的集合的数组
//是否影响老数组:不影响
//Polyfill:衬垫代码
if (!Array.prototype.filter){
Array.prototype.filter = function(fun /* , thisArg*/){"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++){if (i in t){
var val = t[i];
// NOTE: Technically 03_this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (fun.call(thisArg, val, i, t))
res.push(val);}}
return res;};}
</script>
<script type="text/javascript">
//reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。
//数组里所有值的和
var arr =[2, 1, 2, 3];
var sum = arr.reduce(function (a, b){
return a + b;},0);
console.log(sum);
console.log(arr);
//参数1:callback
/*
为数组中每个元素执行的函数,该函数接收三个参数:
accumulator(当前值):累加器累加回调的返回值; 它是上一次调用回调时返回的累积值
currentValue:数组中正在处理的元素。
currentIndex可选:数组中正在处理的当前元素的索引。 如果提供了initialValue,则索引号为0,否则为索引为1。
array可选:调用reduce的数组。
*/
//参数2:initialValue
/*
用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。
在没有初始值的空数组上调用 reduce 将报错。
*///返回值: 一个新的通过测试的元素的集合的数组
//是否影响老数组:不影响
//Polyfill:衬垫代码
if (!Array.prototype.reduce){Object.defineProperty(Array.prototype, 'reduce',{value:function(callback /*, initialValue*/){if (this === null){
throw new TypeError('Array.prototype.reduce ' +
'called on null or undefined');}if (typeof callback !== 'function'){
throw new TypeError( callback +
' is not a function');}
var o = Object(this);
var len = o.length >>> 0;
var k = 0;
var value;
if (arguments.length >= 2){
value = arguments[1];}else{while (k < len && !(k in o)){
k++;}if (k >= len){
throw new TypeError('Reduce of empty array ' +
'with no initial value');}
value = o[k++];}while (k < len){if (k in o){
value = callback(value, o[k], k, o);}
k++;}
return value;}});}
</script>
数组去重
<script type="text/javascript">
//sort + map
var arr = [1,2,1,2,3,5,4,5,3,4,4,4,4];
function qucong(arr){
var result = [];
arr.sort().map(function(item){if(result.length===0 || result[result.length-1]!==item ){
result.push(item);}})
return result;}
console.log(qucong(arr));
//sort + reduce
var arr = ["展示","展示",2,1,2,3,5,4,5,3,4,4,4,4];
var result = arr.sort().reduce(function(init, current){if(init.length===0 || init[init.length-1]!==current){
init.push(current);}
return init;}, []);
console.log(result); //[1,2,3,4,5]
</script>
稀疏数组
<script type="text/javascript">
var a = new Array( 3 );
var b = [ undefined, undefined, undefined ];
var c = [];
c.length = 3;
console.log(a.join("-")); // "--"
console.log(b.join("-")); // "--"
console.log(c.join("-")); // "--"
a.map(function(v,i){ console.log(v,i)});//无输出
b.map(function(v,i){ console.log(v,i)});//有输出
c.map(function(v,i){ console.log(v,i)});//无输出
</script>