千家信息网

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

发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,这篇文章主要介绍了JavaScript数组、字符串和数学函数实例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JavaScript数组、字符串和数学函数实例分析文章都
千家信息网最后更新 2025年11月09日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数组、字符串和数学函数实例分析"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

数组 函数 元素 字符 代码 字符串 方法 数字 数学 功能 参数 实例 实例分析 分析 长度 办法 女装 整数 春装 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全防诈骗安全教案幼儿园 网络技术工程师月薪3万 外文期刊的数据库 网络安全预警级别可分为 新罗区雨花台网络技术服务部 深圳智能软件开发电话 网络技术与经济的结合 沙特阿美网络安全 服务器光驱不好使 人渣scum服务器阿根廷怎么开 有什么软件定时上传数据库到云端 讲述身边的网络安全故事 人工智能大数据网络安全 深圳工业软件开发大概要多少钱 巴南区网络软件开发服务特点 软件开发有没有研究所 供电所信息网络安全会议 2017国家网络安全法 驻点软件开发可以做吗 连接美国香港服务器 供电公司网络安全教育培训 违反网络安全法可以行政拘留吗 我的世界pc版建筑服务器 从数据模型看数据库发展历程 学习软件开发技术哪个好 万得数据库导出上限多少 电脑游戏数据库服务器在哪 云服务器管理控制台下载 自动投注挂机软件开发教程 代理服务器 高校
0