数组遍历的三种方法以及数组排序的sort方法
2017-04-05 12:17
585 查看
var arr = [1,2,3,4,5,6];
第一种:for循环
for(var i=0;i<arr.length;i++){
console.log(arr[i]);
}
第二种:for-in
for(var i in arr){
console.log(arr[i]);
}
第三种
arr.forEach(function(item){
console.log(item);
}
);
数组排序的sort方法
arr.sort(函数名)
arr.sort(sortFun);
//自己定义规则进行排序:按照从小到大
function sortFun(v1 ,v2){
return v1>v2?1:-1;
}
第一种:for循环
for(var i=0;i<arr.length;i++){
console.log(arr[i]);
}
第二种:for-in
for(var i in arr){
console.log(arr[i]);
}
第三种
arr.forEach(function(item){
console.log(item);
}
);
数组排序的sort方法
arr.sort(函数名)
arr.sort(sortFun);
//自己定义规则进行排序:按照从小到大
function sortFun(v1 ,v2){
return v1>v2?1:-1;
}
相关文章推荐
- Java中解锁数组正确姿势以及赋值,foreach遍历?,Java自带的对数组排序,比较等等的静态方法总结
- OC-NSArray数组的三种遍历方式和排序方法
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
- js中数组的sort()方法用法,参数以及排序原理
- Javascript数组的排序 sort()方法和reverse()方法
- 一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- 使用Array的Sort()方法对数组进行排序
- 艾伟_转载:数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- List<T>泛型数组API自带的Sort() 排序方法根据自定义排序实现
- JavaScript sort() 方法数组排序数字【每日一段代码93】
- 用sort()方法对数组的元素进行排序(可按字母升序降序与获得最大最小值)
- 三种方法对java数组排序
- JavaScript数组sort方法(数组排序)
- Javascript数组的排序:sort()方法和reverse()方法
- 使用sort()方法实现数组排序
- PHP中数组的三种排序方法分享
- [php] PHP中遍历关联数组的三种方法
- python数组遍历三种实用方法
- java数组排序Arrays.sort,以及结合Comparator接口的用法
- Javascript数组的排序 sort()方法和reverse()方法