數(shù)組方法里的push,pop,shift,unshift,join,split分別有什么作用寞蚌。
- push在數(shù)組的末端添加一個(gè)或多個(gè)元素田巴,并返回添加新元素后的數(shù)組長(zhǎng)度,該方法會(huì)改變?cè)瓟?shù)組挟秤。
var arr = [];
arr.push('a'); //1
arr.push(2); //2
arr.push(false,{}); //4
arr //['a',2,false,{}]
- pop用于刪除數(shù)組的最后一個(gè)元素壹哺,并返回該元素。該方法會(huì)改變?cè)瓟?shù)組艘刚。
var arr = ['a','c','d'];
arr.pop(); //'d'
arr //['a','c']
[].pop(); //undefined
- shift用于刪除數(shù)組的第一個(gè)元素并返回該元素管宵,會(huì)改變?cè)瓟?shù)組。
var arr = ['a','b','c'];
arr.shift(); //'a'
arr //['b','c']
- unshift用于在數(shù)組的第一個(gè)位置添加元素攀甚,返回添加元素后的數(shù)組長(zhǎng)度箩朴,會(huì)改變數(shù)組。
var arr = ['a','b','c'];
arr.unshift('d'); //4
arr //['d','a','b','c']
- join方法一參數(shù)為分隔符秋度,將所有數(shù)組成員組成一個(gè)字符串返回炸庞,不提供參數(shù)時(shí),默認(rèn)用逗號(hào)分隔荚斯。
var arr = [1,2,3,4];
arr.join(''); //'1234'
arr.join(|); //'1|2|3|4'
arr.join(); //'1,2,3,4'
- split方法按照給定的規(guī)則分割字符串埠居,返回一個(gè)由字符串組成的數(shù)組查牌。
var str = 'a|b|c';
arr.split(''); //[a,|,b,|,c]
arr.split('|'); //[a,b,c]
代碼
用 splice 實(shí)現(xiàn) push、pop滥壕、shift纸颜、unshift方法。
function push (arr,val){
arr.splice(arr.length,0,val);
return arr.length;
}
function pop (arr){
var tmpArr = arr.splice(arr.length-1,1);
return tmpArr[0];
}
function shift(arr) {
var tmpArr = arr.splice(0, 1);
return tmpArr[0];
}
function unshift(arr, val){
arr.splice(0, 0, val);
return arr.length;
}
使用數(shù)組拼接出如下字符串
var prod = {
name:'女裝',
style:['短款','冬季','春裝']
};
function getTplStr(data){
var arr = [];
arr.push('<dl class="product">');
arr.push('<dt>'+data.name+'</dt>');
for (var i = 0; i < data.style.length; i++) {
arr.push('<dd>'+data.style[i]+'</dd>');
}
arr.push('</dt>');
return arr.join(' ');
};
var result = getTplStr(prod);
console.log(result);
<dl class="product">
<dt>女裝</dt>
<dd>短款</dd>
<dd>冬季</dd>
<dd>春裝</dd>
</dl>
寫一個(gè)find函數(shù)绎橘,實(shí)現(xiàn)下面的功能
var arr = ["test", 2, 1.5, false]
function find(array, val){
for (var i = 0; i < array.length; i++) {
if (array[i] === val) {
return i;
}
}
return -1;
}
console.log(find(arr, "test")); // 0
console.log(find(arr, 2)); //1
console.log(find(arr, 1.5)); //2
console.log(find(arr, false)); //3
console.log(find(arr, 3)); //-1
寫一個(gè)函數(shù)filterNumericInPlace胁孙,實(shí)現(xiàn)如下功能
arr = ["a", "b", 1, 3, 4, 5, "b", 2];
function filterNumericInPlace(arr){
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'number') {
newArr.push(arr[i]);
}
}
return newArr;
}
newArr = filterNumericInPlace(arr);
console.log(newArr)
對(duì)象obj有個(gè)className屬性,里面的值為的是空格分割的字符串(和html元素的class特性類似)金踪,寫addClass浊洞、removeClass函數(shù)牵敷,有如下功能
var obj = {
className: 'open menu'
}
addClass(obj, 'new') //obj.className='open menu new'
addClass(obj, 'open') //因?yàn)閛pen已經(jīng)存在胡岔,此操作無(wú)任何變化
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') //不變
var obj = {
className: 'open menu'
}
function addClass(obj,cls){
var arr = obj.className.split(' ');
for (var i = 0; i < arr.length; i++) {
if (arr[i] === cls) {
return;
}
}
arr.push(cls);
obj.className = arr.join(' ');
}
function removeClass(obj,cls){
var arr = obj .className.split(' ');
for (var i = 0; i < arr.length; i++) {
if (arr[i] === cls) {
arr.splice(i,1);
i--;
}
}
obj.className = arr.join(' ');
}
addClass(obj, 'new')
addClass(obj, 'open')
addClass(obj, 'me')
console.log(obj.className)
removeClass(obj, 'open')
removeClass(obj, 'blabla')
console.log(obj.className)
寫一個(gè)camelize函數(shù),把my-short-string形式的字符串轉(zhuǎn)化成myShortString形式的字符串
function camelize(str){
var arr = str.split('-');
for (var i = 1; i < arr.length; i++) {
arr[i] = arr[i].substr(0,1).toUpperCase() + arr[i].substr(1);
}
var newStr = arr.join('');
return newStr;
}
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]()
控制臺(tái)輸出hello hunger valley 彈出undefined
1 arr.push()將一個(gè)函數(shù)加載數(shù)組最后邊靶瘸。
2 arr[arr.length-1]() 將這個(gè)函數(shù)調(diào)用。
3 函數(shù)在控制臺(tái)打印 hello hunger valley, console.log()沒(méi)有返回值毛肋,alert彈出undefined怨咪。
寫一個(gè)函數(shù)filterNumericInPlace,過(guò)濾數(shù)組中的數(shù)字徒溪,刪除非數(shù)字
arr = ["a","b",1,3,4,5,"b",2];
function filterNumericInPlace(arr){
for (var i = 0; i < arr.length; i++) {
if (typeof(arr[i]) !== 'number') {
arr.splice(i, 1);
i--;
}
}
}
filterNumericInPlace(arr);
console.log(arr)
寫一個(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(people){
people.sort(function(a,b){return a.age-b.age;});
}
ageSort(people);
console.log(people)
寫一個(gè)filter(arr, func)函數(shù)用于過(guò)濾數(shù)組然痊,接受兩個(gè)參數(shù)鳍鸵,第一個(gè)是要處理的數(shù)組,第二個(gè)參數(shù)是回調(diào)函數(shù)(回調(diào)函數(shù)遍歷接受每一個(gè)數(shù)組元素匠楚,當(dāng)函數(shù)返回true時(shí)保留該元素,否則刪除該元素)厂财。實(shí)現(xiàn)如下功能
function filter(arr,func){
for (var i = 0; i < arr.length; i++) {
if (!func(arr[i])) {
arr.splice(i,1);
i--;
}
}
return arr;
}
function isNumber(el){
return typeof (el) === "number";
}
var arr = ["a",3,4,true,-1,2,"b"]
arr = filter(arr,isNumber); //arr = [3,4,,-1,2]
arr = filter(arr,function(val){ return val>0}); //arr = [3,4,2]
寫一個(gè) ucFirst函數(shù)芋簿,返回第一個(gè)字母為大寫的字符
function ucFirst(str){
var newStr = str.slice(0,1).toUpperCase()+str.slice(1);
return newStr;
}
console.log(ucFirst("hunger"))
寫一個(gè)函數(shù)truncate(str, maxlength), 如果str的長(zhǎng)度大于maxlength,會(huì)把str截?cái)嗟絤axlength長(zhǎng)璃饱,并加上...
function truncate(str,maxlength){
if (str.length > maxlength) {
return str.slice(0,maxlength) + "...";
}else {
return str;
}
}
console.log(truncate("hello, this is hunger valley,",10))
console.log(truncate("hello,world",20))
寫一個(gè)函數(shù)limit2与斤,保留數(shù)字小數(shù)點(diǎn)后兩位,四舍五入荚恶, 如
function limit2(num1){
return Math.round(num1*100)/100;
}
var num1 = 3.456;
console.log(limit2(num1));
console.log(limit2(2.42));
寫一個(gè)函數(shù)撩穿,獲取從min到max之間的隨機(jī)數(shù),包括min不包括max
寫一個(gè)函數(shù)谒撼,獲取從min都max之間的隨機(jī)整數(shù)食寡,包括min包括max
寫一個(gè)函數(shù),獲取一個(gè)隨機(jī)數(shù)組嗤栓,數(shù)組中元素為長(zhǎng)度為len冻河,最小值為min箍邮,最大值為max(包括)的隨機(jī)數(shù)
function getRandomArbitrary(min,max){
return Math.random()*(max-min)+min;
}
console.log(getRandomArbitrary(2,9));
function getRandomInt(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
console.log(getRandomInt(2,9));
function getRandomArray(len,min,max){
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(Math.random()*(max-min+1)+min);
}
return arr;
}
console.log(getRandomArray(8,2,9))