問題:
<h5>數(shù)組方法里push、pop集峦、shift伏社、unshift、join塔淤、split分別是什么作用?</h5>
<p>
push用于數(shù)組末尾添加一個(gè)元素洛口,同時(shí)數(shù)組length加1;
pop用于數(shù)組末尾刪除一個(gè)元素凯沪,同時(shí)數(shù)組length減1第焰;
shift用于數(shù)組頭部刪除一個(gè)元素,同時(shí)數(shù)組length減1妨马;
unshift用于數(shù)組頭部添加一個(gè)元素挺举,同時(shí)數(shù)組length加1;
join用于使用特定字符將數(shù)組分隔成字符串烘跺,不修改原數(shù)組內(nèi)容湘纵;
split用于把一個(gè)字符串分割成字符串?dāng)?shù)組,不修改原數(shù)組內(nèi)容
<img src="http://upload-images.jianshu.io/upload_images/2304286-378f7f1e557ffa5b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
</p>
1. 用 splice 實(shí)現(xiàn) push滤淳、pop梧喷、shift、unshift方法
var a = [1,2,3,4,5]; function push(arr , val){ arr.splice(arr.length , 1 , val); } function pop(arr){ arr.splice(arr.length-1 , 1); } function shift(arr){ arr.splice(0 , 1); } function unshift(arr , val){ arr.splice(0 , 1 , val); }
2. 使用數(shù)組拼接出如下字符串
var prod = { name: '女裝', styles: ['短款', '冬季', '春裝'] }; function getTplStr(data){ var clothes = []; clothes.push('<dl class="product">\n'); clothes.push('<dt>+data.name+</dt>\n'); var i; for(i=0;i<data.styles.length;i++){ clothes.push('<dd>'+data.styles[i]+'</dd>\n'); } clothes.push('</dl>\n'); return clothes; }; var result = getTplStr(prod); //result為下面的字符串 console.log(result.join(''));
/*<dl class="product"> <dt>女裝</dt> <dd>短款</dd> <dd>冬季</dd> <dd>春裝</dd> </dl>*/
3. 寫一個(gè)find函數(shù)脖咐,實(shí)現(xiàn)下面的功能
/* var arr = [ "test", 2, 1.5, false ] find(arr, "test") // 0 find(arr, 2) // 1 find(arr, 0) // -1 */
代碼:
var arr = [ "test" , 2 , 1.5 , false ]; function find(surch , val){ return surch.indexOf(val); } find(arr , "test"); find(arr , 2); find(arr , 0);
4. 寫一個(gè)函數(shù)filterNumeric铺敌,實(shí)現(xiàn)如下功能
arr = ["a", 1,3,5, "b", 2]; newarr = filterNumeric(arr); // [1,3,5,2]
代碼:
arr = ["a", 1,3,5, "b", 2]; var newar; function filterNumeric(arr){ return arr.filter(function(e){ return typeof(e) == "number"; }) } newarr = filterNumeric(arr); console.log(newarr);
5. 對(duì)象obj有個(gè)className屬性,里面的值為的是空格分割的字符串(和html元素的class特性類似)屁擅,寫addClass偿凭、removeClass函數(shù),有如下功能:
var obj = { className: 'open menu' } function addClass(obj , val){ var arry = obj.className.split(' '); for(var i=0;i<arry.length;i++) { if (arry[i] == val) { obj.className=arry.join(' '); break; return; } else { arry.push(val); obj.className=arry.join(' '); return; } } } function removeClass(obj , val){ var arry = obj.className.split(' '); for(var i=0;i<arry.length;i++) { if (arry[i] == val){ arry.splice(i,1); obj.className=arry.join(' '); break; return; } else { obj.className=arry.join(' '); return; } } } addClass(obj, 'new') // obj.className='open menu new' addClass(obj, 'open') // 因?yàn)閛pen已經(jīng)存在派歌,此操作無任何辦法 addClass(obj, 'me') // obj.className='open menu new me' console.log(obj.className) // "open menu new me" removeClass(obj, 'open') //obj.className='menu new me' removeClass(obj, 'blabla') // 不變
6. 寫一個(gè)camelize函數(shù)弯囊,把my-short-string形式的字符串轉(zhuǎn)化成myShortString形式的字符串,如:
camelize("background-color") == 'backgroundColor' camelize("list-style-image") == 'listStyleImage'
代碼:
function camelize(arry){ newArry = arry.split('-'); console.log(newArry.join(' ')); return newArry.join(' '); } camelize('my-short-string');
7. 如下代碼輸出什么胶果?為什么?
arr = ["a", "b"]; arr.push( function() { alert(console.log('hello hunger valley')) } );//將函數(shù)添加至數(shù)組arr的最末尾匾嘱,數(shù)組元素+1; arr[arr.length-1]() // 調(diào)用數(shù)組arr的最后一個(gè)元素早抠,函數(shù)立即執(zhí)行霎烙,彈窗undefined,打印‘hello hunger valley’
8. 寫一個(gè)函數(shù)filterNumericInPlace,過濾數(shù)組中的數(shù)字吼过,刪除非數(shù)字
arr = ["a",1,3,4,5,"b",2];//對(duì)原數(shù)組進(jìn)行操作锐秦,不需要返回值filterNumericInPlace(arr); console.log(arr) // [1,3,4,5,2]
代碼:
arr = ["a", 1,3,4,5, "b", 2];//對(duì)原數(shù)組進(jìn)行操作,不需要返回值function filterNumericInPlace(arry){ for(var i=0;i<arry.length;i++){ if(typeof arry[i]=='string'){ arr.splice(i,1) } } } filterNumericInPlace(arr); console.log(arr) // [1,3,4,5,2]
9. 寫一個(gè)ageSort函數(shù)實(shí)現(xiàn)如下功能
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 ] ageSort(people) // [ bob, mary, john ]
代碼:
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(arr){ arr.reverse(); } ageSort(people) // [ bob, mary, john ]
10. 寫一個(gè)filter(arr, func)函數(shù)用于過濾數(shù)組盗忱,接受兩個(gè)參數(shù)酱床,第一個(gè)是要處理的數(shù)組,第二個(gè)參數(shù)是回調(diào)函數(shù)(回調(diào)函數(shù)遍歷接受每一個(gè)數(shù)組元素趟佃,當(dāng)函數(shù)返回true時(shí)保留該元素扇谣,否則刪除該元素)。實(shí)現(xiàn)如下功能:
代碼:
function isNumeric (el){ return typeof el === 'number'; } function filter(arr, func){ return arr.filter(func); } arr = ["a",3,4,true, -1, 2, "b"] arr = filter(arr, isNumeric) ; //arr = [3,4,-1,2], 過濾出數(shù)字arr = filter(arr, function(val) { return val > 0 }); // arr = [3, 4, 2]] 過濾出大于0的整數(shù)
字符串
11. 寫一個(gè) ucFirst函數(shù)闲昭,返回第一個(gè)字母為大寫的字符
ucFirst("hunger") == "Hunger"
代碼:
function ucFirst(str){ str = str.split(''); str[0] = str[0].toUpperCase(); console.log(str.join('')) return str.join('') } ucFirst('hello')
12. 寫一個(gè)函數(shù)truncate(str, maxlength), 如果str的長度大于maxlength罐寨,會(huì)把str截?cái)嗟絤axlength長,并加上...序矩,如:
truncate("hello, this is hunger valley,", 10)) == "hello, thi..."; truncate("hello world", 20)) == "hello world"
代碼:
function truncate(str , maxlength){ str = str.split('') if(str.length>maxlength){ str.splice(maxlength-1,str.length-maxlength+1,"...") return str.join('') } else console.log( str.join('')) return str.join('') } var a = "hello,this is hunger"; truncate(a,10) truncate(a,20)
數(shù)學(xué)函數(shù)
13. 寫一個(gè)函數(shù)limit2鸯绿,保留數(shù)字小數(shù)點(diǎn)后兩位,四舍五入簸淀, 如:
var num1 = 3.456 limit2( num1 ); //3.46 limit2( 2.42 ); //2.42
代碼:
function limit2(num){ console.log( ( Math.round(num*100) )/100 ) return ( Math.round(num*100) )/100; } var num1 = 3.456 limit2( num1 ); //3.46 limit2( 2.42 ); //2.42
14. 寫一個(gè)函數(shù)瓶蝴,獲取從min到max之間的隨機(jī)數(shù),包括min不包括max代碼:
function getNum(min,max){ return min+(max-min)*Math.random() }
15. 寫一個(gè)函數(shù)租幕,獲取從min都max之間的隨機(jī)整數(shù)舷手,包括min包括max
function getNum(min,max){ return Math.floor(min)+Math.round( ( Math.ceil(max)-Math.floor(min) )*Math.random() ); }
16. 寫一個(gè)函數(shù),獲取一個(gè)隨機(jī)數(shù)組劲绪,數(shù)組中元素為長度為len男窟,最小值為min,最大值為max(包括)的隨機(jī)數(shù)
function getArr(len,min,max){ arr = []; for(var i=0;i<len;i++){ arr.push( (min+(max-min)*Math.random() ); } console.log(arr); return arr; } getArr(len,min,max)
本文版權(quán)歸簾外修竹所有贾富,轉(zhuǎn)載必須注明出處