JS数组、字符串及数学函数
本篇将会对js数组和字符串以及函数进行讲解。
数组方法里push、pop、shift、unshift、join、split分别是什么作用
push:在数组最后添加一个元素,语法是数组.push (所要添加的元素);,返回值为数组长度
pop: 删除数组最后一个元素,语法为数组.pop( );返回值为删除的元素名称
shift:删除数组第一个元素,语法为数组.shift( );返回值为删除的元素名称
unshift:在数组首位添加一个元素,后面元素向后偏移,语法为数组.unshift (所要添加的元素);,返回值为数组长度
join:将数组连接为字符串,不修改原本的数组,语法为数组.join(),返回值为连接完成的字符串
split: 将字符串分隔并变为数组,不修改原本的字符串,语法为字符串.split('分隔符');
代码:
用 splice 实现 push、pop、shift、unshift 方法
splice实现push:
new1
[91, 3, 2, 1, 34, 5] //new1数组的元素new1.splice(new1.length,0,91) //new1.length代表数组最后一位后面,0为关键字添加,91为所要添加元素[]
new1
[91, 3, 2, 1, 34, 5, 91] //成功在数组new1最后添加元素91
用splice实现pop:
new1
[91, 3, 2, 1, 34, 5, 9, 91] //new1数组的元素new1.splice(new1.length-1,1) //new1.length代表数组最后一位,1为长度[91]
new1
[91, 3, 2, 1, 34, 5, 9] //成功删除最后一位元素91
splice实现shift:
new1
[91, 3, 2, 1, 34, 5, 645] //new1数组的元素new1.splice(0,1) //0代表数组下标索引数,1代表删除个数[91] //返回删除的元素new1
[3, 2, 1, 34, 5, 645] //成功删除new1数组第一个元素
splice实现unshift:
new1
[3, 2, 1, 34, 5, 645] //new1数组的元素new1.splice(0,0,91) //第一个0代表数组下标索引数,第二个0为关键字添加,91为所要添加的元素[]
new1
[91, 3, 2, 1, 34, 5, 645] //成功在数组第一位添加元素91
使用数组拼接出如下字符串
var prod = { name: '女装', styles: ['短款', '冬季', '春装'] };function getTp(data){ var new1 = prod.name; var new2 = prod.styles; var arrey =[]; arrey.push('<dl class="product">'); arrey.push("<dt>"+new1+"</dt>"); for(var i =0;i<new2.length;i++){ arrey.push("<dd>"+new2[i]+"</dd>") } arrey.push('</dl>'); console.log(arrey.join()); } getTp(prod)//<dl class="product">,<dt>女装</dt>,<dd>短款</dd>,<dd>冬季</dd>,<dd>春装</dd>,</dl>
写一个find函数,实现下面的功能
var arr = [ "test" , 2 , 1.5 , false ]function find(arr,add){ for(var i = 0;i < arr.length; i++){ if(add==arr[i] && add !== 0){ return console.log(i) } } console.log(-1) } find(arr, "test") // 0find(arr, 2) // 1find(arr, 0) // -1
写一个函数filterNumeric,实现如下功能
arr = ["a", 1,3,5, "b", 2];var arre = [];function filterNumeric(arr){ for(var i= 0; i< arr.length;i++){ if(typeof arr[i]==='number'){ arre.push(arr[i]) } } console.log(arre); } filterNumeric(arr) //[1, 3, 5, 2]
对象obj有个className属性,里面的值为的是空格分割的字符串(和html元素的class特性类似),写addClass、removeClass函数,有如下功能:
var obj = { className: 'open menu'}; var shu = obj.className.split(" "); function addClass(obj,nano){ for(var i = 0;i< shu.length; i++) { if(shu[i] === nano) { return console.log("因为"+nano+"已经存在,此操作无任何办法"); } } shu.push(nano); //console.log(shu); obj.className = shu.join(" "); console.log(obj); } addClass(obj, 'new'); //Object {className: "open menu new"}addClass(obj, 'open'); //因为open已经存在,此操作无任何办法addClass(obj, 'me'); // Object {className: "open menu new me"}console.log(obj.className); // open menu new mefunction removeClass(obj,habo){ //console.log(shu) for(var i = 0;i<shu.length;i++){ if(shu[i] === habo) { shu.splice(i,1); } } obj.className = shu.join(' '); console.log(obj); } removeClass(obj,"open"); //Object {className: "menu new me"}removeClass(obj, 'blabla'); //Object {className: "menu new me"}
写一个camelize函数,把my-short-string形式的字符串转化myShortString形式的字符串
function camelize(lama){ var lala = lama.split("-"); //["list", "style", "image"] var a =[lala[0]]; for(var i =1; i<lala.length; i++) { var num =lala[i][0].toUpperCase(); //"S", "I" var b = lala[i].replace(lala[i][0],num) a.push(b) }console.log(a.join("")) } camelize("background-color") //"backgroundColor"camelize("list-style-image") //"listStyleImage""
如下代码输出什么?为什么?
arr = ["a", "b"]; arr.push( function() { alert(console.log('hello hunger valley')) } ); arr[arr.length-1]() //
输出的是function函数的内容'hello hunger valley'并弹出窗口显示underfined。因为第二段直接将整个函数添加到数组arr后面成为它最后一个元素,最后一句代表将arr数组的最后一个元素执行调用,console.log执行完会销毁,所以打印结果为'hello hunger valley',而弹窗结果为underfined
写一个函数filterNumericInPlace,过滤数组中的数字,删除非数字
arr = ["a", 1 , 3 , 4 , 5 , "b" , 2];function filterNumericInPlace(arr){ for(var i= 0; 0< arr.length; i++) { if( typeof arr[i] !== 'number'){ arr.splice(i,1) } } console.log(arr); // [1,3,4,5,2]} filterNumericInPlace(arr);
写一个ageSort函数实现如下功能
var john = { name: "John Smith", age: 23 };var mary = { name: "Mary Key", age: 18 };var bob = { name: "Bob-small", age: 6 };var people = [ john, mary, bob ];function ageSort(data){ data.sort(function (a,b){ return a.age-b.age; }) console.log(data); } ageSort(people); // [ bob, mary, john ]
写一个filter(arr, func)函数用于过滤数组,接受两个参数,第一个是要处理的数组,第二个参数是回调函数(回调函数遍历接受每一个数组元素,当函数返回true时保留该元素,否则删除该元素)
function isNumeric (el){ return typeof el === 'number'; } arr = ["a",3,4,true, -1, 2, "b"];function filter(arr, func){ for(var i =0;i<arr.length;i++){ if(!func(arr[i])){ arr.splice(i,1); } } return arr; } arr = filter(arr, isNumeric) ;console.log(arr); arr = filter(arr,function(val){ return val > 0});console.log(arr); [3, 4, -1, 2] [3, 4, 2]
字符串
写一个 ucFirst函数,返回第一个字母为大写的字符
function ucFirst(daho){ var add= daho[0].toUpperCase(); daho=daho.replace(daho[0],add) console.log(daho); } ucFirst("hunger")"Hunger"
写一个函数truncate(str, maxlength), 如果str的长度大于maxlength,会把str截断到maxlength长,并加上...,如
function truncate(str, maxlength){ if(str.length-1>maxlength){ add = str.substr(0,maxlength); console.log(add+"..."); }else{ return console.log(str); } } truncate("hello, this is hunger valley,", 10) truncate("hello world", 20)"hello, thi...""hello world"
数学函数
写一个函数limit2,保留数字小数点后两位,四舍五入, 如:
var num1 = 3.456;function limit2(num){ num=Math.round(num*100)/100; console.log(num); } limit2(num1) limit2(2.42)3.462.42
写一个函数,获取从min到max之间的随机数,包括min不包括max
function habo(min,max){ console.log(Math.random()*(min-max)+max) } habo(5,15)
写一个函数,获取从min都max之间的随机整数,包括min包括max
function habo(min,max){ add = Math.random()*(min-max)+max; console.log(Math.round(add)); } habo(5,12)
写一个函数,获取一个随机数组,数组中元素为长度为len,最小值为min,最大值为max(包括)的随机数
function damo(len,min,max){ arr=[]; len == arr.length; for(var i =0;i<len;i++){ arr.push(Math.round(Math.random()*(min-max)+max)); } console.log(arr) } damo(5,18,70) [53, 34, 43, 43, 33]
本篇对JS数组、字符串及数学函数相关内容进行了讲解,更多相关知识请关注php中文网。
相关推荐:
以上是JS数组、字符串及数学函数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Go语言提供了两种动态函数创建技术:closures和反射。closures允许访问闭包作用域内的变量,而反射可使用FuncOf函数创建新函数。这些技术在自定义HTTP路由器、实现高度可定制的系统和构建可插拔的组件方面非常有用。

在C++函数命名中,考虑参数顺序至关重要,可提高可读性、减少错误并促进重构。常见的参数顺序约定包括:动作-对象、对象-动作、语义意义和遵循标准库。最佳顺序取决于函数目的、参数类型、潜在混淆和语言惯例。

1、 SUM函数,用于对一列或一组单元格中的数字进行求和,例如:=SUM(A1:J10)。2、AVERAGE函数,用于计算一列或一组单元格中的数字的平均值,例如:=AVERAGE(A1:A10)。3、COUNT函数,用于计算一列或一组单元格中的数字或文本的数量,例如:=COUNT(A1:A10)4、IF函数,用于根据指定的条件进行逻辑判断,并返回相应的结果。

C++函数中默认参数的优点包括简化调用、增强可读性、避免错误。缺点是限制灵活性、命名限制。可变参数的优点包括无限灵活性、动态绑定。缺点包括复杂性更高、隐式类型转换、调试困难。

C++中的函数返回引用类型的好处包括:性能提升:引用传递避免了对象复制,从而节省了内存和时间。直接修改:调用方可以直接修改返回的引用对象,而无需重新赋值。代码简洁:引用传递简化了代码,无需额外的赋值操作。

自定义PHP函数与预定义函数的区别在于:作用域:自定义函数仅限于其定义范围,而预定义函数可在整个脚本中访问。定义方式:自定义函数使用function关键字定义,而预定义函数由PHP内核定义。参数传递:自定义函数接收参数,而预定义函数可能不需要参数。扩展性:自定义函数可以根据需要创建,而预定义函数是内置的且无法修改。

C++中的异常处理可通过定制异常类增强,提供特定错误消息、上下文信息以及根据错误类型执行自定义操作。定义继承自std::exception的异常类,提供特定的错误信息。使用throw关键字抛出定制异常。在try-catch块中使用dynamic_cast将捕获到的异常转换为定制异常类型。实战案例中,open_file函数抛出FileNotFoundException异常,捕捉并处理该异常可提供更具体的错误消息。
