JavaScript数组_第1页
免费预览已结束,剩余14页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、javascript数组数组办法里push、pop、shift、unshift、join、split分离是什么作用。push()办法添加一个或多个元素到数组的末尾,并返回数组新的长度(length属性值)。pop()办法删除一个数组中的最后的一个元素,并且返回这个元素。shift()办法删除数组的第一个元素,并返回这个元素。该办法会转变数组的长度。unshift()办法在数组的开始添加一个或者多个元素,并返回数组新的length值。join()办法将数组中的全部元素衔接成一个字符串。*split()*办法通过把字符串分割成子字符串来把一个string对象分割成一个字符串数组。代码题数组用spl

2、ice实现push、pop、shift、unshift办法定义和使用splice()办法用于插入、删除或替换数组的元素。语法arrayobject.splice(index,howmany,element1,.,elementx)参数描述index必须。规定从何处添加/删除元素。该参数是开头插入和(或)删除的数组元素的下标,必需是数字。howmany必须。规定应当删除多少元素。必需是数字,但可以是"0"。假如未规定此参数,则删除从index开头到原数组结尾的全部元素。element1可选。规定要添加到数组的新元素。从index所指的下标处开头插入。elementx可选。可向

3、数组添加若干元素。返回值假如从arrayobject中删除了元素,则返回的是含有被删除的元素的数组。splice-pushvara=1,2,3,4,5varb=1,2,3,4,5console.log(a);console.log(b);a.push(6);b.splice(5,1,6);console.log(a);console.log(b);splice-popvara=1,2,3,4,5varb=1,2,3,4,5console.log(a);console.log(b);a.pop();b.splice(4,1);console.log(a);console.log(b);splic

4、e-shiftvara=1,2,3,4,5varb=1,2,3,4,5console.log(a);console.log(b);a.shift();b.splice(0,1);console.log(a);console.log(b);splice-unshiftvara=1,2,3,4,5varb=1,2,3,4,5console.log(a);console.log(b);a.unshift(-1);b.splice(0,0,-1);console.log(a);console.log(b);用法数组拼接出如下字符串varprod=name:&39;女装&39;,styles:&39;短

5、款&39;,&39;冬季&39;,&39;春装&39;functiongettpl(data)/todo.;varresult=gettplstr(prod);/result为下面的字符串女装短款冬季春装代码:varprod=name:&39;女装&39;,styles:&39;短款&39;,&39;冬季&39;,&39;春装&39;functiongettplstr(data)varhtmls=;htmls.push(&39;&39;,&39;&39;+data,name+&39;&39;);for(i=0;i&39;+data.stylesi+&39;&39;)htmls.push(&39

6、;&39;);varhtmls=htmls.join(&39;&39;)returnhtmls;varresult=gettplstr(prod);/result为下面的字符串console.log(result)写一个find函数,实现下面的功能vararr="test",2,1.5,falsefind(arr,"test")/0find(arr,2)/1find(arr,0)/-1代码:vararr="test",2,1.5,falsevarfind=function(a,b)console.log(a.indexof(b)fin

7、d(arr,"test")/0find(arr,2)/1find(arr,0)/-1写一个函数filternumeric,实现如下功能arr="a",1,3,5,"b",2;newarr=filternumeric(arr);/1,3,5,2代码:办法一:arr="a",1,3,5,"b",2;varfilternumberic=function(data)vara=;for(i=0;i0)varb=ai;ai=aj;aj=b;returnavarjohn=name:"johnsmit

8、h",age:23varmary=name:"marykey",age:18varbob=name:"bob-small",age:6varpeople=john,mary,bobagesort(people)/bob,mary,johnconsole.log(agesort(people)写一个filter(arr,func)函数用于过滤数组,接受两个参数,第一个是要处理的数组,其次个参数是回调函数(回调函数遍历接受每一个数组元素,当函数返回true时保留该元素,否则删除该元素)。实现如下功能:functionisnumeric(el)ret

9、urntypeofel=&39;number&39;arr="a",3,4,true,-1,2,"b"arr=filter(arr,isnumeric);/arr=3,4,-1,2,过滤出数字arr=filter(arr,function(val)returnval0);/arr=2过滤出大于0的整数代码:functionfilter(data,callback)returndata.filter(callback)functionisnumeric(el)returntypeofel=&39;number&39;arr="a",3,

10、4,true,-1,2,"b"arr=filter(arr,isnumeric);/arr=3,4,-1,2,过滤出数字console.log(arr)arr=filter(arr,function(val)returnval0);/arr=2过滤出大于0的整数console.log(arr)字符串写一个ucfirst函数,返回第一个字母为大写的字符。ucfirst("hunger")="hunger"代码:functionucfirst(string)returnstring0.touppercase()+string.slice(

11、1);console.log(ucfirst("hunger")ucfirst("hunger")="hunger"写一个函数truncate(str,maxlength),假如str的长度大于maxlength,会把str截断到maxlength长,并加上.,如:truncate("hello,thisishungervalley,",10)="hello,thi."truncate("helloworld",20)="helloworld"代码:fun

12、ctiontruncate(str,maxlength)if(str.lengthmaxlength)varsub=str.substring(maxlength)str=str.replace(sub,&39;.&39;);returnstr;console.log(truncate("hello,thisishungervalley,",10);truncate("hello,thisishungervalley,",10)="hello,thi."truncate("helloworld",20)="

13、;helloworld"数学函数写一个函数limit2,保留数字小数点后两位,四舍五入,如:varnum1=3.456limit2(num1);/3.46limit2(2.42);/2.42代码:varnum1=3.456functionlimit2(data)varnum=math.round(data*100);returnnum/100limit2(num1);/3.46limit2(2.42);/2.42console.log(limit2(num1);console.log(limit2(2.42);console.log(limit2(-1.15555555)写一个函数,猎取从min到max之间的随机数,包括min不包括max。代码:functionfun(min,max)returnmin+math.random()*(max-min)console.log(fun(5,10)写一个函数,猎取从min都max之间的随机整数,包括min包括max。代码:functionfun(min,max)returnmath.round(min+math.random()*(max-min)console.log(fu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论