资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JavaScript数组、字符串和数学函数实例分析

这篇文章主要介绍了JavaScript数组、字符串和数学函数实例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JavaScript数组、字符串和数学函数实例分析文章都会有所收获,下面我们一起来看看吧。

为灵山等地区用户提供了全套网页设计制作服务,及灵山网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、外贸网站建设、灵山网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

push()方法添加一个或多个元素到数组的末尾,并返回数组新的长度(length属性值)。

pop()方法删除一个数组中的最后的一个元素,并且返回这个元素。

shift()方法删除数组的第一个元素,并返回这个元素。该方法会改变数组的长度。

unshift()方法在数组的开头添加一个或者多个元素,并返回数组新的length值。

join()方法将数组中的所有元素连接成一个字符串。

**split()**方法通过把字符串分割成子字符串来把一个String对象分割成一个字符串数组。

代码题

数组

用splice实现push、pop、shift、unshift方法

定义和用法

splice()方法用于插入、删除或替换数组的元素。

语法

arrayObject.splice(index,howmany,element1,.....,elementX)

参数描述

index必需。规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。

howmany必需。规定应该删除多少元素。必须是数字,但可以是"0"。如果未规定此参数,则删除从index开始到原数组结尾的所有元素。element1可选。规定要添加到数组的新元素。从index所指的下标处开始插入。

elementX可选。可向数组添加若干元素。

返回值

如果从arrayObject中删除了元素,则返回的是含有被删除的元素的数组。

splice->push

vara=[1,2,3,4,5]

varb=[1,2,3,4,5]

console.log(a);

console.log(b);

a.push(6);

b.splice(5,1,6);

console.log(a);

console.log(b);

splice->pop

vara=[1,2,3,4,5]

varb=[1,2,3,4,5]

console.log(a);

console.log(b);

a.pop();

b.splice(4,1);

console.log(a);

console.log(b);

splice->shift

vara=[1,2,3,4,5]

varb=[1,2,3,4,5]

console.log(a);

console.log(b);

a.shift();

b.splice(0,1);

console.log(a);

console.log(b);

splice->unshift

vara=[1,2,3,4,5]

varb=[1,2,3,4,5]

console.log(a);

console.log(b);

a.unshift(-1);

b.splice(0,0,-1);

console.log(a);

console.log(b);

使用数组拼接出如下字符串

varprod={name:'女装',styles:['短款','冬季','春装']

};functiongetTpl(data){//todo...};varresult=getTplStr(prod);//result为下面的字符串

女装

短款

冬季

春装

代码:

varprod={

name:'女装',

styles:['短款','冬季','春装']

};

functiongetTplStr(data){

varhtmls=[];

htmls.push('','

'+data,name+'
');

for(i=0;i

htmls.push('

'+data.styles[i]+'
')

}

htmls.push('

');

varhtmls=htmls.join('')

returnhtmls

};

varresult=getTplStr(prod);//result为下面的字符串

console.log(result)

写一个find函数,实现下面的功能

vararr=["test",2,1.5,false]

find(arr,"test")//0

find(arr,2)//1

find(arr,0)//-1

代码:

vararr=["test",2,1.5,false]

varfind=function(a,b){

console.log(a.indexOf(b))

}

find(arr,"test")//0

find(arr,2)//1

find(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;i

if(typeofdata[i]==='number'){

a.push(data[i]);

}

}

returna

}

newarr=filterNumberic(arr);//[1,3,5,2]

console.log(newarr)

方法二:

arr=["a",1,3,5,"b",2];

functionisNumber(element){

returntypeofelement==='number';

}

varnewarr=arr.filter(isNumber)

console.log(newarr)

对象obj有个className属性,里面的值为的是空格分割的字符串(和html元素的class特性类似),写addClass、removeClass函数,有如下功能:

varobj={className:'openmenu'}addClass(obj,'new')//obj.className='openmenunew'addClass(obj,'open')//因为open已经存在,此操作无任何办法addClass(obj,'me')//obj.className='openmenunewme'console.log(obj.className)//"openmenunewme"

removeClass(obj,'open')//obj.className='menunewme'removeClass(obj,'blabla')//不变

代码:

varobj={className:'openmenu'}varaddClass=function(a,b){varname=a.className.split("");if(name.indexOf(b)===-1){name.push(b);}else{console.log("因为"+b+"已经存在,此操作无任何办法");}a.className=name.join("");console.log('obj.className='+a.className);}varremoveClass=function(a,b){varname=a.className.split("");if(name.indexOf(b)!==-1){name.splice(name.indexOf(b),1)a.className=name.join("");console.log('obj.className='+a.className)}else{console.log('不变')}}

addClass(obj,'new')//obj.className='openmenunew'addClass(obj,'open')//因为open已经存在,此操作无任何办法addClass(obj,'me')//obj.className='openmenunewme'console.log(obj.className)//"openmenunewme"removeClass(obj,'open')//obj.className='menunewme'removeClass(obj,'blabla')//不变

写一个camelize函数,把my-short-string形式的字符串转化成myShortString形式的字符串,如:

camelize("background-color")=='backgroundColor'

camelize("list-style-image")=='listStyleImage'

代码:

functioncamelize(string){

returnstring.replace(/-/g,'')

}

console.log(camelize("background-color"))

camelize("background-color")=='backgroundColor'

camelize("list-style-image")=='listStyleImage'

如下代码输出什么?为什么?

arr=["a","b"];

arr.push(function(){alert(console.log('hellohungervalley'))});

arrarr.length-1//?

因为arr.push(function(){alert(console.log('hellohungervalley'))});将function(){alert(console.log('hellohungervalley')push到arr[]最后一位,arr[arr.length-1]()取该数组最后一位,然后立即执行该函数,由于function(){alert(console.log('hellohungervalley')中console.log只允许在控制台中打开,所以结果为undefined。

写一个函数filterNumericInPlace,过滤数组中的数字,删除非数字

arr=["a",1,3,4,5,"b",2];

//对原数组进行操作,不需要返回值

filterNumericInPlace(arr);

console.log(arr)//[1,3,4,5,2]

代码:

arr=["a","d",1,3,4,5,"b",2];

//对原数组进行操作,不需要返回值

functionfilterNumericInPlace(data){

for(i=0;i

if(typeofdata[i]==='string'){

data.splice(i,1);

i--;//splice指针减少1,否则获取不了数组中全部元素。

}

}

}

filterNumericInPlace(arr);

console.log(arr)//[1,3,4,5,2]

写一个ageSort函数实现如下功能:

varjohn={name:"JohnSmith",age:23}

varmary={name:"MaryKey",age:18}

varbob={name:"Bob-small",age:6}

varpeople=[john,mary,bob]

ageSort(people)//[bob,mary,john]

代码:

方法一:

functionageSort(arr){

arr.sort(function(a,b){returna.age-b.age})

returnarr

}

varjohn={name:"JohnSmith",age:23}

varmary={name:"MaryKey",age:18}

varbob={name:"Bob-small",age:6}

varpeople=[john,mary,bob]

ageSort(people)//[bob,mary,john]

console.log(ageSort(people))

方法二:

functionageSort(a){

for(i=0;i

for(j=i+1;j

if(a[i].age-a[j].age>0){

varb=a[i];

a[i]=a[j];

a[j]=b;

}

}

}

returna

}

varjohn={name:"JohnSmith",age:23}

varmary={name:"MaryKey",age:18}

varbob={name:"Bob-small",age:6}

varpeople=[john,mary,bob]

ageSort(people)//[bob,mary,john]

console.log(ageSort(people))

写一个filter(arr,func)函数用于过滤数组,接受两个参数,第一个是要处理的数组,第二个参数是回调函数(回调函数遍历接受每一个数组元素,当函数返回true时保留该元素,否则删除该元素)。实现如下功能:

functionisNumeric(el){returntypeofel==='number';}arr=["a",3,4,true,-1,2,"b"]

arr=filter(arr,isNumeric);//arr=[3,4,-1,2],过滤出数字arr=filter(arr,function(val){returnval>0});//arr=[2]过滤出大于0的整数

代码:

functionfilter(data,callback){returndata.filter(callback)}

functionisNumeric(el){returntypeofel==='number';}arr=["a",3,4,true,-1,2,"b"]arr=filter(arr,isNumeric);//arr=[3,4,-1,2],过滤出数字console.log(arr)arr=filter(arr,function(val){returnval>0});//arr=[2]过滤出大于0的整数console.log(arr)

字符串

写一个ucFirst函数,返回第一个字母为大写的字符。

ucFirst("hunger")=="Hunger"

代码:

functionucFirst(string){

returnstring[0].toUpperCase()+string.slice(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"

代码:

functiontruncate(str,maxlength){

if(str.length>maxlength){

varsub=str.substring(maxlength)

str=str.replace(sub,'...');

}returnstr;

}

console.log(truncate("hello,thisishungervalley,",10));

truncate("hello,thisishungervalley,",10)=="hello,thi...";

truncate("helloworld",20)=="helloworld"

数学函数

写一个函数limit2,保留数字小数点后两位,四舍五入,如:

varnum1=3.456

limit2(num1);//3.46

limit2(2.42);//2.42

代码:

varnum1=3.456

functionlimit2(data){

varnum=Math.round(data*100);

returnnum/100

}

limit2(num1);//3.46

limit2(2.42);//2.42

console.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(fun(5,10))

写一个函数,获取一个随机数组,数组中元素为长度为len,最小值为min,最大值为max(包括)的随机数.

代码:

functionfun(min,max,leng){

vararr=[]

for(i=0;i

varvalue=max-Math.random()*(max-min)

arr.push(value)

}

returnarr

}

console.log(fun(5,10,6))

关于“JavaScript数组、字符串和数学函数实例分析”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“JavaScript数组、字符串和数学函数实例分析”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


当前标题:JavaScript数组、字符串和数学函数实例分析
文章地址:http://cdkjz.cn/article/gogpjg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220