数组
1.扩展运算符的应用
合并数组
// 1.合并数组
let array=[1, 2, 3]
let array2=[4, 5, 6]
array.push(...array2)
console.log(array)//[1, 2, 3, 4, 5, 6]
let array3=['a', 'b', 'c']
let array4=['d', 'e', 'f']
Array.prototype.push.apply(array3, array4)
console.log(array3)// ["a", "b", "c", "d", "e", "f"]
let array5=['a', 'b', 'c']
let array6=['d', 'e', 'f']
let res=[...array5,...array6]
console.log(res)// ["a", "b", "c", "d", "e", "f"]
求数组最大值
// 2.求数组最大值
let numberarray=[5,6,52,4,89,21]
let resmax=Math.max.apply(this,numberarray)
console.log(resmax) //89
let resmax2=Math.max.call(this,...numberarray)
console.log(resmax2) //89
let resmax3=Math.max(...numberarray)
console.log(resmax3) //89
复制数组
let arraycope=[1,5,3,56]
let arraycope1=[...arraycope]
arraycope1[0]=100
console.log(arraycope1)// [100, 5, 3, 56]
console.log(arraycope)// [1, 5, 3, 56]
let [...arraycope2]=arraycope
console.log(arraycope2) //[1, 5, 3, 56]
将字符串转为数组
let string='hello'
let stringchangearray=[...'hello']
console.log(stringchangearray)//["h", "e", "l", "l", "o"]
2.Array.of()将一组织转化为数组
let arraychange=Array.of(2,5,8,5,2)
console.log(arraychange)//[2, 5, 8, 5, 2]
3.find()和findIndex()用于找到第一个符合条件的成员和成员对应的索引值
// find() findindex()
let findarray=[1,5,8,9,6]
let resfind=findarray.find((item)=>item>5)
console.log(resfind)//8 返回的是具体值
let resfindindex=findarray.findIndex((item)=>item>5)
console.log(resfindindex)//2 返回的是位置
4.数组的实例方法
// 数组实例方法 keys() valus() entries()
let newarrayfun=['a','b','c']
let resarrkey=Object.keys(newarrayfun)
console.log(resarrkey)//["0", "1", "2"]
let resarrvalue=Object.values(newarrayfun)
console.log(resarrvalue)//["a", "b", "c"]
let resarrentries=Object.entries(newarrayfun)
console.log(resarrentries)//[["0", "a"],["1", "b"],["2", "c"]]
5.数组的空位
// 数组的空位
let havenullarr=['a',,'b']
// forEach filter every reduce some会跳过空格
havenullarr.forEach((item,index)=>console.log(item)) //a b
console.log(['a',,'b'].filter((item,index)=>true)) //["a", "b"]
console.log(['a',,].every(item=>item==='a'))//true 所有符合要求返回true否则false
console.log(['a',,'b'].reduce((x,y)=>x+y))//ab
console.log(['a',,'b'].some(item=>item==='a'))//true 有一个符合返回true,否则 false
// map会跳过这个空格,保留这个值
console.log(['a',,'b'].map((item)=>item=1))//[1, , 1]
// join toString会将空格视为undefined 被处理成空字符串
console.log(['a',,'b'].join())//a,,b
console.log(['a',,'b'].toString())//a,,b
6.数组的排序稳定性
// sort
let sortarray=['peach','straw','apple','spork']
let sortarr=sortarray.sort()
console.log(sortarr) //["apple", "peach", "spork", "straw"]
const stableSorting = (s1, s2) => {
if (s1[0] < s2[0]) return -1;
return 1;
}
let funressort=sortarray.sort(stableSorting)
console.log(funressort)//["apple", "peach", "straw", "spork"]
对象
1.对象的遍历
let obj={
name:'lily',
age:18,
city:'山西',
classname:'一班'
}
// 1.for...in循环遍历对象自身的和继承的可枚举属性(不含 Symbol 属性)。
for(let a in obj){
console.log(a)
}
// 2.Object.keys返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含 Symbol属性)的键名。
let newarray=Object.keys(obj)
console.log(newarray)//["name", "age", "city", "classname"]
// 3.Object.getOwnPropertyNames返回一个数组,包含对象自身的所有属性(不含 Symbol 属性,但是包括不可枚举属性)的键名
let newarray2=Object.getOwnPropertyNames(obj)
console.log(newarray2)//["name", "age", "city", "classname"]
// 4.Object.getOwnPropertySymbols返回一个数组,包含对象自身的所有 Symbol 属性的键名。
let newarray3=Object.getOwnPropertySymbols(obj)
console.log(newarray3)//[]
// 5.Reflect.ownKeys返回一个数组,包含对象自身的(不含继承的)所有键名,不管键名是 Symbol 或字符串,也不管是否可枚举。
let newarray4=Reflect.ownKeys(obj)
console.log(newarray4)//["name", "age", "city", "classname"]
2.解构赋值
对象的解构赋值属于浅拷贝
// 对象的解构赋值属于浅拷贝
let obj2={name:{x:1}}
let {...test}=obj2
console.log(test)
test.name.x=9
console.log(obj2)//name:{x:9}
3.扩展运算符
扩展运算符可以用来合并对象
// 扩展运算符
let z={a:3,b:4}
let n={...z}
console.log(n)//{a: 3, b: 4}
let foo1={...['a','b','c']}
console.log(foo1)//{0: "a", 1: "b", 2: "c"}
console.log({...'hello'})//{0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
let a={x:1}
let b={y:1}
console.log({...a,...b})//合并对象{x: 1, y: 1}
Object.assign(a,b)
console.log(a)//合并对象{x: 1, y: 1}
4.object.assign()
object.assign()遇到同名属性会覆盖
object.assign()属于浅拷贝
object.assign()为对象添加属性 添加方法
object.assign()克隆对象,合并多个对象
// object.assign()合并对象
let a2={x:1}
let b2={y:1}
console.log({...a2,...b2})//合并对象{x: 1, y: 1}
let a1={x:1}
let b1={y:1}
Object.assign(a1,b1)
console.log(a1)//合并对象{x: 1, y: 1}
Object.assign(a1,5)
console.log(a1)//{x: 1, y: 1}
let c1={y:2}
Object.assign(a1,c1)
console.log(a1)//{x: 1, y: 2},遇到同名的属性会覆盖
let str='abc'
let str2=true
let str3=123
const obj3=Object.assign({},str,str2,str3)
console.log(obj3)//{0: "a", 1: "b", 2: "c"}
let obj4={
name:'lily',
age:12,
classinfo:{
name:'一班',
people:20
}
}
let obj5={
city:'山西'
}
Object.assign(obj5,obj4)
obj4.name='天津'
console.log(obj5)
obj4.classinfo.name='二班'
console.log(obj5)//obj5.classinfo.name='二班' assign是浅拷贝
let funobj={
get foo(){return 2}
}
let objfun={}
Object.assign(objfun,funobj)
console.log(objfun)//{foo: 2} 取值函数的处理
// 为对象添加属性
let o={q:0}
Object.assign(o,{z:3})
console.log(o)//{q: 0, z: 3}
let objarr=[1,2,3]
let objarr2=[4,5]
Object.assign(objarr,objarr2)
console.log(objarr)//[4, 5, 3] 可以用来处理数组 Object.assign()把数组视为属性名
为 0、1、2 的对象 覆盖
// 为对象添加方法
function objfun1(){return 1}
function objfun3(){return 3}
let obj6={
name:'lily'
}
Object.assign(obj6,{objfun1,objfun3})
console.log(obj6)//{name: "lily", objfun1: ƒ, objfun3: ƒ}
// 克隆
function clone(origin){
return Object.assign({},origin)
}
let cloneojb={name:'clone'}
console.log(clone(cloneojb))//{name: "clone"}
// 合并多个对象
let soiurceobj={age:18}
const merge=(target,source)=>Object.assign(source,target)
console.log(merge(cloneojb,soiurceobj))//{age: 18, name: "clone"}