/**
* 变量交换
* 使用数组解构来直接交换值
* @type {number}
*/
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b)
/**
* 对象解构
* 通过结构访问对象属性,而不需要使用繁琐的 obj.property 语法
*/
const {name, age} = {name: '张三', age: 18}
console.log(name, age)
/**
* 对象扩展
* 使用对象扩展语法,可以很方便的将一个对象的属性复制到另一个对象
*/
const obj = {
name: '张三',
age: 18
}
const obj2 = {
...obj,
sex: '男'
}
console.log(obj2)
const obj3 = {
...obj
}
console.log(obj3)
/**
* 数组过滤
* @type {number[]}
*/
const arr1 = [1, 2, 3, 4, 5, 5, 6, 6, 6, 7, 8]
const arr2 = arr1.filter(item => item > 3)
console.log(arr2)
/**
* 数组判断,有任意元素满足条件
* @type {boolean}
*/
const hasOne = arr1.some(item => item === 1)
console.log(hasOne)
/**
* 数组判断,所有元素都满足条件
* @type {this is number[]}
*/
const allIsTrue = arr1.every(item => item > 2)
console.log(allIsTrue)
/**
* 数组的交际
* @type {number[]}
*/
const arr4 = [6, 7, 8]
const arr5 = [1, 2, 3, 4, 5, 6, 7, 8]
const arr6 = arr4.filter(item => arr5.includes(item))
console.log(arr6)
/**
* 数组的并集
* @type {number[]}
*/
const arr7 = [...new Set([...arr4, ...arr5])]
console.log(arr7)
/**
* 数组的差集
* @type {number[]}
*/
const arr8 = arr5.filter(item => !arr4.includes(item))
console.log(arr8)
/**
* 数组的补集
* @type {number[]}
*/
const arr9 = arr4.filter(item => !arr5.includes(item)).concat(arr5.filter(item => !arr4.includes(item)))
console.log(arr9)
/**
* 数组的排序
* @type {number[]}
*/
const arr10 = [1, 2, 3, 4, 5, 6, 7, 8]
arr10.sort((a, b) => a - b)
console.log(arr10)
/**
* 数组的反转
* @type {number[]}
*/
const arr11 = [1, 2, 3, 4, 5, 6, 7, 8]
arr11.reverse()
console.log(arr11)
/**
* 数组的去重
* @type {number[]}
*/
const arr12 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,8, ]
const arr13 = [...new Set(arr12)]
console.log(arr13)
/**
* 数组求和
*/
const arr14 = [1, 2, 3, 4,]
const sum = arr14.reduce((a, b) => a + b)
console.log(sum)
/**
* 数组求最大值
*/
const arr15 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const max = arr15.reduce((a, b) => a > b ? a : b)
console.log(max)
/**
* 数组求最小值
*/
const arr16 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const min = arr16.reduce((a, b) => a < b ? a : b)
console.log(min)
/**
* 数组求平均值
*/
const arr17 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const avg = arr17.reduce((a, b) => a + b) / arr17.length
console.log(avg)
/**
* 动态属性名
*/
const obj4 = {
[name]: age
}
console.log(obj4)
/**
* 对象数组求和
*/
const arr18 = [
{
name: '张三',
age: 18
},
{
name: '李四',
age: 19
},
{
name: '王五',
age: 20
}
]
const sum2 = arr18.reduce((a, b) => a + b.age, 0)
console.log(sum2)
/**
* 秒转时间
*/
const second = 3600
const time = new Date(second * 1000).toISOString().substr(11, 8)
console.log(time)
/**
* 小时转秒
*/
const hour = 1
const second2 = hour * 3600
console.log(second2)
/**
* 秒转小时
*/
const second3 = 3600
const hour2 = second3 / 3600
console.log(hour2)
/**
* 秒转天
*/
const second4 = 86400
const day = second4 / 86400
console.log(day)
/**
* 天转秒
*/
const day2 = 1
const second5 = day2 * 86400
console.log(second5)
/**
* 秒转分
*/
const second6 = 60
const minute = second6 / 60
console.log(minute)
/**
* 分转秒
*/
const minute2 = 1
const second7 = minute2 * 60
console.log(second7)
网友评论