/* for 是否全部大于0 */ var ary = [12,23,24,42,1]; var result = function(){ for (var i = 0; i < ary.length; i++) { if(ary[i] < 0){ returnfalse; } } returntrue; } console.log(result()) // true
1 2 3 4 5 6
/* every */ var ary = [12,23,24,42,1]; var result = ary.every(function(item, index){ return item > 0 }) console.log(result)
4、some()
some()可以检测数组中是否有某一项符合条件
1 2 3 4 5 6 7 8 9 10 11
/* for 是否存在小于0的项 */ var ary = [12,23,-24,42,1]; var result = function(){ for (var i = 0; i < ary.length; i++) { if(ary[i] < 0){ returntrue; } } returnfalse; } console.log(result()) //有一项小于0,返回true
1 2 3 4 5 6
/* some 是否存在小于0的项 */ var ary = [12,23,-24,42,1]; var result = ary.some(function(item, index){ return item < 0 }) console.log(result)
console.log("No Additional parameter:",noPassValue()); console.log("----------------"); console.log("With {} as an additional parameter:",passValue());
/* 使用此方法反转字符串中的字符 */ var word = "retupmoc"; functionAppendToArray(previousValue, currentValue) { return previousValue + currentValue; } var result = [].reduceRight.call(word, AppendToArray, "the "); console.log(result); /* the computer */
isArray()
isArray()是Array对象的一个静态函数,用来判断一个对象是不是数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var ary1 = []; var res1 = Array.isArray(ary1); /* Output: true */ console.log(res1)
var ary2 = newArray(); var res2 = Array.isArray(ary2); /* Output: true */ console.log(res2)
var ary3 = [1, 2, 3]; var res3 = Array.isArray(ary3); /* Output: true */ console.log(res3)
var ary4 = newDate(); var res4 = Array.isArray(ary4); /* Output: false */ console.log(res4)