//Number2String var string2="13579" var theStringArray=[] for (const stringElement of string2) { theStringArray.push(stringElement) } var theNumberArray=theStringArray.map(Number) console.log(theNumberArray) var number= theNumberArray.reduce(function(x, y) { return x * 10+y; }) console.log(number)
var thefilter=[2,2,31,23,21,3,214,3,4,5,3254,7,8,8,945,352,9,5] var theNewArray=thefilter.filter(function (x) { return x%2==0 }) console.log(theNewArray)
注意此处操作,返回的值是一个新的数组,重新赋值操作理解
1 2 3 4 5 6 7
var theTrimfilter=["thisthings","",null,undefined,""] var theNewArray=thefilter.filter(function(x) { //ie9下没有这个方法,自然会报错! //仍然还是基于浏览器进行的操作理解啊 return x && x.trim() }) console.log(theNewArray)
很多版本的操作还是需要优质操作的!
筛选相同元素,这种操作确实是让人觉得眼前一亮
1 2 3 4 5 6 7 8
var theStringArray=["jack","queen","king","jack","queen","king","master"] //对于回调函数的理解就是简单的操作进行的! var theNewArray2=theStringArray.filter(function (element,index,self) { //不相等的理解操作,如何实现也是非常必要的。这里返回的是真值操作 return self.indexOf(element)==index }) console.log(theNewArray2)
var arr=[] for (var i = 1; i < 600; i++) { arr.push(i) } //筛选质数的很多操作也是令人沉迷 var theSuArray=arr.filter(function (element,index,self){ if (element==1){ returnfalse } for (var i = 2; i <element;i++ ) { if (element%i==0) { returnfalse } } returntrue }) console.log(theSuArray)