函数名和变量名相同时
//如果函数名和变量名相同,函数会把变量作用域覆盖
console.log(num)
var num = 10;
function num(){
console.log("hellow word")
}
/*
var num = function(){
console.log("hellow word")
}
*/
console.log(num) //函数
var num =10;
function num(){
console.log("hellow")
}
console.log(num); //10
function num(a){
/*
var a =function(){
console.log("hellow word")
}
*/
console.log(a); //function
var a = 10;
function a(){
console.log("hellow word")
}
console.log(a); //10
}
num(20);
function关键字声明
/*使用function关键字声明的函数,会将整段函数声明提前
*/
console.log(go);
go = 10;
console.log(go);
function go(){
console.log("hellow word")
声明提前中函数名和变量名字相同的情况下
function num(a){
console.log(a);
var a =10;
function a(){
console.log("hellow")
}
console.log(a);
}
num(20);
/*
var a = 10;
*/
删除的方法
方法1
var arr = [1,2,3]
arr.pop();
/*从后删除*/
console.log(arr)
方法2 从前删除
var arr = [2,3,4]
arr.shift();
console.log(arr)
方法3
/*shift pop*/
var arr = [4,5,6,7,8]
/*
splice(index,howmany)
index--从那个下标开始删除
howmany删除几个
*/
arr.splice(2,2)
console.log(arr)
修改语句
/*
修改 "vue"
splice(index,howmany,value)
index 下标
howma 删除多少个
value 替代的值
*/
var arr = ["html","css","javascript"];
arr.splice(1,1,"vue")
console.log(arr)
includes的方法判断
var arr = [1,2,3,4,5,6,7,8,9];
console.log(arr.includes(6));
console.log(arr.includes(9));
/*splice 增加
从下标的前面添加
splice(index,0,value...)
*/
var arr = [3,4,5,6]
arr.splice(2,0,"html","css");
console.log(arr)
增,删,改
/*增,删,改,*/
var arr = [3,4,5,6];
/*
[3,4,"html",5,6]
splice(index,0,value)
*/
arr.splice(2,0,"html");
console.log(arr)
/*
删除
splice(index,howmany)
*/
var test = [1,2,3,4,5]
//[1,4,5]
test.splice(1,2);
console.log(test)
var arr = [2,3,4,5]
//[2,3,"vue",5]
//arr.splice(index,howmany...value)
arr.splice(2,1,"vue")
console.log(arr)
查询
/*
indexOf
-1
*/
// var arr = [2,3,4];
// console.log(arr.indexOf(2));
// console.log(arr.indexOf(8));
var arr = [2,3,4,5,6];
var test = arr.slice(1,3);
//slice(startIndex,endIndex) tips:不包含结束的下标
console.log(test)
var arr = [1,2,3,4,5,8];
var sum = 0;
for(var i=0;i<arr.length;i++){
sum +=arr[i];
}
console.log(sum)
练习
var arr =[
{name:"李四",age:"18岁"},
{name:"高四",age:"20岁"},
{name:"樟四",age:"8岁"},
{name:"李四",age:"16岁"},
]
var test = [];
for(var i=0;i<arr.length;i++){
//console.log(arr[i].age)
var age = parseInt(arr[i].age);
console.log(age)
if(age>16){
console.log(arr[i])
test.push(arr[i])
}
}
console.log(test)
var arr = [1,2,3,4,5,6,7,8,9]
//[[1,2,3],[4,6,7],[7,8,9]]
// var newArr = [];
// var len = arr.length;
// for(let i =0,j =0; i<len; i+=9,j++){
// newArr[j] = arr.splice(0,8);
// }
// console.log(newArr)
/*slice(0,3) 0-2
slice(3,6) 3-5
slice(6,0) 6-8
*/
var test = [];
for(var i=0;i<arr.length;i+=3){
//console.log(i)
test.push(arr.slice(i,i+3))
}
console.log(test)
求最小值
var arr = [4,6,18,1];
//alert(Math.min.apply(null,arr))
//console.log(Math.min.apply(null,arr))
/*假设 var min = arr[0]*/
var min =arr [0];
for(var i=1;i<arr.length;i++){
if(min>arr[i]){
min =arr[i];
}
}
console.log(min)
示例
var arr = [
{name:"lisi",age:"12px"},
{name:"wangwn",age:"13px"},
{name:"zhangsan",age:"14px"},
]
for(var i=0;i<arr.length;i++){
// var age = parseInt(arr[i].age);
// console.log(age)
var age = parseInt(arr[i].age)*2
//console.log(age)
arr[i].age = age;
arr[i].like = true;
if(arr[i].age>24){
arr[i].like = true;
}else{
arr[i].like= false
}
}
console.log(arr)
// var str = "你武汉";
// console.log(str.includes("武"))
var searchs =[
{
spell:"A",
citys:[
{name:"阿里",id:1001},
{name:"阿拉",id:1001},
{name:"阿克",id:1001}
]
},{
spell:"T",
citys:[
{name:"天下",id:2001},
{name:"天天",id:2002},
{name:"天里",id:2003}
]
}
]
//var keyword = "里";
var arr = [];
var input = document.getElementById("input");
//获取关键字
input.onkeydown = function(event){
if(event.keyCode ==13){
if(this.value !=""){
var kw = this.value.trim();
//使用关键字对数组进行遍历
for (var i=0;i<searchs.length;i++){
var citys = searchs[i].citys;
for ( var j = 0;j < citys.length;j++){
if (citys[j].name.includes(kw)){
arr.push(citys[j]);
}
}
}
console.log(arr)
}
}
}
trim去除字符串前后尾的空格
var str = " fdsf "
console.log(str.trim())