码迷,mamicode.com
首页 > 编程语言 > 详细

数组方法

时间:2016-07-09 10:40:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

map:

var oldArray = [1,2,3,4,5];
var newArray = oldArray.map(function(val){
  return val +=3;//return [4,5,6,7,8]
});

reduce:

var array = [4,5,6,7,8];
var singleVar = 0;
singleVar = array.reduce(function(pre,cur){
    return pre+cur;//return 4+5+6+7=8+9=30
});

filter:

var oldArr = [1,2,3,4,5,6,7,8,9,10];
var newArr = oldArr.filter(function(val){
    return val<6;//return [1,2,3,4,5]
});

sort:

var arr = [1,12,21,3];
array.sort(function(a,b){
    return a-b;//from small to big
});

reverse:

var arr= [1,2,3,4,5,6,7];
var newArr = [];
newArr = arr.reverse();//return [7,6,5,4,3,2,1]

concat:

var oldArr = [1,2,3];
var newArr = [];
var concatMe = [4,5,6];
newArr = oldArr.concat(concatMe);//return [1,2,3,4,5,6]

split:

var String = "Split me into an array";
var array =[];
array = string.split(" ");//return ["Split","me","into","an","array"]

join:

var joinMe = ["Split","me","into","an","array"];
var joinedString ="";
joinedString = joinMe.join(" ");

 

数组方法

标签:

原文地址:http://www.cnblogs.com/Natsume5233/p/5655273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!