数组方法中的push、pop、shift、unshift、join、split

  • push是推,向数组的末尾添加一个数据;pop是拿掉,把数组中最后一个数去掉;
    push&pop
  • shift是将数组的第一个数移除;unshift是向数组首位添加一个数;
    shift&unshift
  • join是将数组里的每一项用自定义字符串接起来成一个字符串并返回,不会影响原数组;split是将字符串的以选定的字符分隔开并存储在数组中返回,也不会影响原字符串;join&split

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

push&pop
shift&unshift

数组小练习

使用数组做Html的拼接

var prod={
  name:'女装',
  styles:['短款','冬季','春装']
};
function getTpl(data){
  var arr=['<dl class="product">','</dl>'];
  arr.splice(arr.length-1,0,('<dt>'+data.name+'</dt>'));
  for(var i=0;i<data.styles.length;i++){
    arr.splice(arr.length-1,0,('<dd>'+data.styles[i]+'</dd>'));
  }
  return arr.join('');
}
var result=getTpl(prod);
console.log(result);       // <dl class="product"><dt>女装</dt><dd>短款</dd><dd>冬季</dd><dd>春装</dd></dl>

函数filterNumeric,可以找出目标数组中的数字

arr = ["a", 1, "b", 2];
function filterNumberic(arr){
  var newarr=[];
  for(var i in arr){
    if((typeof arr[i]) === 'number')
      newarr.push(arr[i]);
  }
  return newarr;
}

warr=filterNumberic(arr); 
console.log(warr);

addClass、removeClass函数的实现

var obj = {
  className: 'open menu'
};
function addClass(obj,val){
  newarr=obj.className.split(' ');
  for (var i=0; i<newarr.length; i++){
    if(newarr[i] === val){
      console.log(obj.className);
      return ;
    }
  }
  obj.className=obj.className.replace(obj.className[obj.className.length-1],obj.className[obj.className.length-1]+' '+val);
  console.log(obj.className);
}
function removeClass(obj,val){
  newarr=obj.className.split(' ');
  console.log(newarr);
  for (var i=newarr.length-1; i>-1; i--){
    if(newarr[i] === val)
      newarr.splice(i,1);
  } 
  obj.className=newarr.join(' ');
  console.log(obj.className);
}
addClass(obj, 'new'); // obj.className='open menu new'
addClass(obj, 'open');  // 因为open已经存在,此操作无任何办法
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'); // 不变

将字符串首字母大写并去掉连接符‘-’

function camelize(str){
  var arr=str.split('-');
  for(i in arr){
    arr[i]=arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
  }
  str=arr.join('')
  console.log(str);
}
camelize("background-color"); // BackgroundColor
camelize("list-style-image"); // ListStyleImage

Sort函数依据age将对象排序

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.sort(function(a,b){
    return a.age - b.age;
  });
  console.log(arr);
}
ageSort(people) // [ bob, mary, john ]

字符串小练习

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

function ucFirst(char){
  char=char.charAt(0).toUpperCase()+char.slice(1);
  console.log(char);
}
ucFirst("hunger");   //  Hunger

函数truncate(str, maxlength), 如果str的长度大于maxlength,会把str截断到maxlength长,并加上…

function turncate(str,maxlength){
  if(str.charAt(maxlength)!=='')
  str=str.slice(0,maxlength)+'...';
console.log(str);
}
turncate("hello, this is hunger valley,", 10); // "hello, thi...";
turncate("hello world", 20); // "hello world"

数学函数小练习

函数limit2,保留数字小数点后两位,四舍五入

function limit2(num){
  num=Math.round(num*100)/100;
  console.log(num);
}
var num1 = 3.456
limit2( num1 );  //3.46
limit2( 2.42 );    //2.42

函数minMax,获取从min到max之间的随机数,包括min不包括max

function minMax(min,max){
  console.log((max-min)*Math.random()+min);  
}
minMax(50,100);  // 93.67403362954349  73.32116993708193   51.95918010535645

函数minMax,获取从min到max之间的随机整数,包括min包括max

function minMax(min,max){
  console.log(Math.floor((max-min+1)*Math.random()+min));  
}
minMax(50,60);  // 57 55 53 52 54 50

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

function getArr(min,max,len){
  var arr=[];
  for(var i=0;i<len;i++){
    arr.push(Math.round(1e14*((max-min)*Math.random()+min))/1e14);  
  }
  console.log(arr);
  return arr;
}
getArr(50,60,3);  // [50.0677831803007, 50.09292999150542, 50.07795163429897]
//把随机数扩大1e14倍,四舍五入后再/1e14,当出现59.99999999999999时近似出现60