- 向数组或对象中插入数据
const arr1 = ['春', '夏', '秋']
arr1.push('冬') // 向数组插入数据
const obj = [{ id: 1 }, { id: 2, season: '夏天' }]
obj[0].season = '春天' // 向对象中插入数据
- 数组-字符串,对象-字符串之间的转换
const a1 = arr1.join(',') // 将数组转化成字符串
const a2 = a1.split(',') // 将字符串转化为数组
const o1 = JSON.stringify(obj) // 将对象转化为字符串
const o2 = JSON.parse(o1) // 将字符串转化为对象
- 对象数组 去重
methods: {
test () {
const arr = [
{ id: 1, name: 'a', number: '100' },
{ id: 1, name: 'b', number: '200' },
{ id: 1, name: 'a1', number: '100' },
{ id: 1, name: 'c', number: '300' }
]
const arr2 = this.unRep(arr)
},
unRep (arr) {
const res = new Map()
return arr.filter((a) => !res.has(a.number) && res.set(a.number, 1))
}
}
- 数组的深度拷贝:
const arr2 = JSON.parse(JSON.stringify(arr))
- 判断是否为有效时间
isValidDate (val) {
const date = new Date(val)
return date instanceof Date && !isNaN(date.getTime())
}
- 把时间转成时间戳
Date.parse(new Date(date1)) - Date.parse(new Date(date2))