JavaScript学习第二天

发布于 2022-08-09  120 次阅读


.### 数组的常用方法

//push后面增加
var arr = [1,2,3]
arr.push(4) //在后面追加元素
console.log(arr) //返回的是{1,2,3,4}
var res = arr.push(4)
console.log("返回值",res) //返回值是数组增加之后的长度
//POP后面删除
var respop = arr.pop()
console.log(arr)
console.loh("返回值",respop) //返回值是删除的元素
//unshift前面追加
var resunshift = arr.unshift("tiechui")
console.log(arr)
console.log("返回值",resunshift) //返回是长度
//shift前面删除
var resshift = arr.shift()
console.log(arr)
console.loh("返回值",resshift) //返回是删除的元素
//splice 自定义删除
var ressplice = arr.splice(1,2) //什么位置开始,删除几个
console.log(arr)
console.log("返回值",splice) //返回的是删除的元素组成的数组
//splice 自定义添加
var ressplice = arr.splice(1,0,"beixi","hikurn") //什么位置开始,删除几个,从那个位置之前加,加什么
console.log(arr)
//resverse 倒叙排序
var arr3 = [1,2,3,4]
arr3.resverse()
console.log(arr3)
//sort 倒序排序
var arr4 =  [11,21,3,56,7]
arr.ressort(function(a,b){
    return(a-b) //a-b 是从小到大,反之从大到小
})
console.log(arr4)
//concat拼接(不影响原数组)
var arr1 = [1,2,3]
var arr2 = [4,5,6]
var arr3 = arr1.concat(arr2)
var arr4 = arr.contcat(arr,7,[8,9])//可以拼接并添加】
var arr5 = arr1.concat()
arr4.pop
//join 数组转换成字符串
var arr6 = [1,2,3,4,5]
document.write(arr6.join("-"))//输出结果就是1-2-3-4
//案例
var arr= []
for(var i=0;i<5;i++){
    arr.push("<li>"+i+"</li>")
}
console.log(arr)
document.write(arr(""))
//slice 截取(不影响原数组)
var arr = ["beixi","tongbao","tangbao","mingbao"]
var arr2 = arr.slice(0,2)
console.log(arr2)//输出结果是"beixi"和"tongbao"
var arr3 = arr.slice(2)
console.log(arr3)//输出结果2到结束是"tangbao","mingbao"
var arr4 = arr.slice()
console.log(arr4)//输出结果就是复制一份,和concat有异曲同工之妙
var arr5 = arr.slice(2,-1)
console.log(arr5)//输出结果是"tangbao"-1是指(数组数量+(-1))
//indexOf/lastOf 返回-1就是找不到(没有)
var arr = ["beixi","tongbao","tangbao","beixi","hikurn"]
var res = arr.indexOf("ccc")//-1
var res = arr.indexOf("beixi") //0(此时后面的beixi就查不到了)
var res = arr.indexOf("beixi",2) //3(从2开始查询,就有机会查询到后面的,但是前面的就不查了)
var res = arr.lastOf("beixi")//1
数组去重
var arr = [1,2,3,4,5,6,2,1]
//1
var arr2 = []
for var(var i=0;i<arr.length;i++){
    if(arr2.indexOf(arr[i])===-1){
        arr2.push(arr[i])
    }
}
//2
var obj = {}
for(var i=0;i<arr.length;i++){
    obj[arr[i]] = "随便"
}
var arr2 = []
for(var i in obj){
    arr2.push(i)
}
console.log(arr2)
//3 new Set
var set1 = new Set(arr)
var arr1 = Array.from(set1)
console.log(set1)
//forEach 遍历
var arr = ["aaa","bbb","ccc","ddd"]
arr.forEach(function(item,index){
    console.log(item,index)
})
//map映射
var arr = [1,2,3,4,5]
var arr2 = arr.map(function(){
    return item*item
})
console.log(arr)//输出结果是各自的平方
//filter过滤
var arr = [100,200,300]
var arr2 = arr.filter(function(item){
    return item>200
})
console.log(arr2)

var arr3 = [{name:beixi,price:100},{name:xiaozhi,price:1}]
var arr4 =arr.filter(function(item){
    return item.price>50
})
console.log(arr4)
//every 每一个
var arr = [80,90,92,94]
arr.every(function(item){
    return item>=90
})
//some 只要一个满足条件就是ture
var arr = [90,90,2,94]
arr.some(function(item){
    return item>=90
})
//find 过滤/找到(找到一个就不管了,对应的是filter)
var arr = [{name:"yuwen",grade:"90"},{name:"shuxue",grade:"90"},{name:"tiyu",grade:"100"}]
var arr2 = arr.find(function(item){
    return item.grade===100
})
//reduce 叠加
var arr = [1,2,3,4]
var arr2 = arr.reduce(function(prev,item){
    return prev+item
},0)
console.log(arr2)

字符串

创建字符串

var str1 = "hello"

var str2 = new String("hello")

字符串的常用方法