js数组的下标不是字符串,是数组;
var arr = [1,5,15,10];
var col = arr.map(functiction(value,index,arr){
return value + index; // 返回一个数组 [1,6,17,13];
});
var arr = [1,2,3,4]; //reduce对数组的(每一次循环时,取相邻元素两个)
var result = arr.reduce(function(pre,next,index,array){
//第一次时,pre指下标0的元素;之后,pre指return的值
console.log('pre:'+pre);
console.log('next:'+next);
console.log('index:'+index);
console.log('array:'+array);
return pre+next; //处理
})
console.log(arr); //[1,2,3,4]
console.log(num);//10
forEach();
var arr = [3,5,8];
var col = arr.forEach(function(value,index){
//console.log(value,index,col);
arr[index] = value*1.3;
})
//console.log(arr);
console.log(col); //underfined
//forEach 只做循环,本身没有返回值
filter() 过滤 var arr = [88,69,92,77,90,96,98,99];
var newarr = arr.filter(function(item, index, array){ return item>80; //找出所有大于80的元素 });
console.log( arr ); // [88, 69, 92, 77, 90, 96, 98, 99]
console.log( newarr ); // [88, 92, 90, 96, 98, 99]
every()每一个
//跟some一样;但要求每一项都符合,才返回true;
//有一项不合格就返回false;
var arr = [1,2,3,4,5];
var b = arr.every(function(item){
return item > 3;
})
console.log(arr); //[1,2,3,4,5,]
console.log(b); //false
some()某些;
数组中的元素,只要有一项返回真,最终的结果就为真。
var arr= ["yt","hr","wc","jk"];
var b= arr.some(function(item){
//判断数组中是否存在yt?
return item == "yt",
})
console.log(arr); //["yt","hr","wc","jk"]
console.log(b); //true