函数名和变量名相同时

  1. //如果函数名和变量名相同,函数会把变量作用域覆盖
  2. console.log(num)
  3. var num = 10;
  4. function num(){
  5. console.log("hellow word")
  6. }
  1. /*
  2. var num = function(){
  3. console.log("hellow word")
  4. }
  5. */
  6. console.log(num) //函数
  7. var num =10;
  8. function num(){
  9. console.log("hellow")
  10. }
  11. console.log(num); //10
  1. function num(a){
  2. /*
  3. var a =function(){
  4. console.log("hellow word")
  5. }
  6. */
  7. console.log(a); //function
  8. var a = 10;
  9. function a(){
  10. console.log("hellow word")
  11. }
  12. console.log(a); //10
  13. }
  14. num(20);

function关键字声明

  1. /*使用function关键字声明的函数,会将整段函数声明提前
  2. */
  3. console.log(go);
  4. go = 10;
  5. console.log(go);
  6. function go(){
  7. console.log("hellow word")

声明提前中函数名和变量名字相同的情况下

  1. function num(a){
  2. console.log(a);
  3. var a =10;
  4. function a(){
  5. console.log("hellow")
  6. }
  7. console.log(a);
  8. }
  9. num(20);
  10. /*
  11. var a = 10;
  12. */

删除的方法

方法1

  1. var arr = [1,2,3]
  2. arr.pop();
  3. /*从后删除*/
  4. console.log(arr)

方法2 从前删除

  1. var arr = [2,3,4]
  2. arr.shift();
  3. console.log(arr)

方法3

  1. /*shift pop*/
  2. var arr = [4,5,6,7,8]
  3. /*
  4. splice(index,howmany)
  5. index--从那个下标开始删除
  6. howmany删除几个
  7. */
  8. arr.splice(2,2)
  9. console.log(arr)

修改语句

  1. /*
  2. 修改 "vue"
  3. splice(index,howmany,value)
  4. index 下标
  5. howma 删除多少个
  6. value 替代的值
  7. */
  8. var arr = ["html","css","javascript"];
  9. arr.splice(1,1,"vue")
  10. console.log(arr)

includes的方法判断

  1. var arr = [1,2,3,4,5,6,7,8,9];
  2. console.log(arr.includes(6));
  3. console.log(arr.includes(9));
  1. /*splice 增加
  2. 从下标的前面添加
  3. splice(index,0,value...)
  4. */
  5. var arr = [3,4,5,6]
  6. arr.splice(2,0,"html","css");
  7. console.log(arr)

增,删,改

  1. /*增,删,改,*/
  2. var arr = [3,4,5,6];
  3. /*
  4. [3,4,"html",5,6]
  5. splice(index,0,value)
  6. */
  7. arr.splice(2,0,"html");
  8. console.log(arr)
  9. /*
  10. 删除
  11. splice(index,howmany)
  12. */
  13. var test = [1,2,3,4,5]
  14. //[1,4,5]
  15. test.splice(1,2);
  16. console.log(test)
  1. var arr = [2,3,4,5]
  2. //[2,3,"vue",5]
  3. //arr.splice(index,howmany...value)
  4. arr.splice(2,1,"vue")
  5. console.log(arr)

查询

  1. /*
  2. indexOf
  3. -1
  4. */
  5. // var arr = [2,3,4];
  6. // console.log(arr.indexOf(2));
  7. // console.log(arr.indexOf(8));
  8. var arr = [2,3,4,5,6];
  9. var test = arr.slice(1,3);
  10. //slice(startIndex,endIndex) tips:不包含结束的下标
  11. console.log(test)
  1. var arr = [1,2,3,4,5,8];
  2. var sum = 0;
  3. for(var i=0;i<arr.length;i++){
  4. sum +=arr[i];
  5. }
  6. console.log(sum)

练习

  1. var arr =[
  2. {name:"李四",age:"18岁"},
  3. {name:"高四",age:"20岁"},
  4. {name:"樟四",age:"8岁"},
  5. {name:"李四",age:"16岁"},
  6. ]
  7. var test = [];
  8. for(var i=0;i<arr.length;i++){
  9. //console.log(arr[i].age)
  10. var age = parseInt(arr[i].age);
  11. console.log(age)
  12. if(age>16){
  13. console.log(arr[i])
  14. test.push(arr[i])
  15. }
  16. }
  17. console.log(test)
  1. var arr = [1,2,3,4,5,6,7,8,9]
  2. //[[1,2,3],[4,6,7],[7,8,9]]
  3. // var newArr = [];
  4. // var len = arr.length;
  5. // for(let i =0,j =0; i<len; i+=9,j++){
  6. // newArr[j] = arr.splice(0,8);
  7. // }
  8. // console.log(newArr)
  9. /*slice(0,3) 0-2
  10. slice(3,6) 3-5
  11. slice(6,0) 6-8
  12. */
  13. var test = [];
  14. for(var i=0;i<arr.length;i+=3){
  15. //console.log(i)
  16. test.push(arr.slice(i,i+3))
  17. }
  18. console.log(test)

求最小值

  1. var arr = [4,6,18,1];
  2. //alert(Math.min.apply(null,arr))
  3. //console.log(Math.min.apply(null,arr))
  4. /*假设 var min = arr[0]*/
  5. var min =arr [0];
  6. for(var i=1;i<arr.length;i++){
  7. if(min>arr[i]){
  8. min =arr[i];
  9. }
  10. }
  11. console.log(min)

示例

  1. var arr = [
  2. {name:"lisi",age:"12px"},
  3. {name:"wangwn",age:"13px"},
  4. {name:"zhangsan",age:"14px"},
  5. ]
  6. for(var i=0;i<arr.length;i++){
  7. // var age = parseInt(arr[i].age);
  8. // console.log(age)
  9. var age = parseInt(arr[i].age)*2
  10. //console.log(age)
  11. arr[i].age = age;
  12. arr[i].like = true;
  13. if(arr[i].age>24){
  14. arr[i].like = true;
  15. }else{
  16. arr[i].like= false
  17. }
  18. }
  19. console.log(arr)
  1. // var str = "你武汉";
  2. // console.log(str.includes("武"))
  3. var searchs =[
  4. {
  5. spell:"A",
  6. citys:[
  7. {name:"阿里",id:1001},
  8. {name:"阿拉",id:1001},
  9. {name:"阿克",id:1001}
  10. ]
  11. },{
  12. spell:"T",
  13. citys:[
  14. {name:"天下",id:2001},
  15. {name:"天天",id:2002},
  16. {name:"天里",id:2003}
  17. ]
  18. }
  19. ]
  20. //var keyword = "里";
  21. var arr = [];
  22. var input = document.getElementById("input");
  23. //获取关键字
  24. input.onkeydown = function(event){
  25. if(event.keyCode ==13){
  26. if(this.value !=""){
  27. var kw = this.value.trim();
  28. //使用关键字对数组进行遍历
  29. for (var i=0;i<searchs.length;i++){
  30. var citys = searchs[i].citys;
  31. for ( var j = 0;j < citys.length;j++){
  32. if (citys[j].name.includes(kw)){
  33. arr.push(citys[j]);
  34. }
  35. }
  36. }
  37. console.log(arr)
  38. }
  39. }
  40. }

trim去除字符串前后尾的空格

  1. var str = " fdsf "
  2. console.log(str.trim())