近期JS心得

child和tags都是[{id:1,value:'a'}]的格式,当点击一级标签,要看二级标签是否已经被选中,如果被选中,则清除出去 如果用for循环 再splice的话 当删除掉了一个元素后,数组的索引发生的变化,造成了程序的异常

所以要用while的方式

const arr = child.childList.map(item => item.id)
let i = this.tags.length
while (i--) {
  if (arr.includes(this.tags[i].id)) {
    this.tags.splice(i, 1)
  }
}

上面的数组对象[{id:1,value:'a'}] 根据id去重

const id = 'id'
this.tags = this.tags.reduce((all, next) => all.some((atom) => atom[id] == next[id]) ? all : [...all, next], [])
数组去重 [1,2,3,4,2,3] 先转成没有重复的set 再转成数组
this.openLabels = Array.from(new Set(this.openLabels))
某个对象的数据渲染了dom,这时要在js里给这个对象添加一个它没有的属性,如果直接用点 elem.haveChecked = 1 这时因为这个属性不是本来就有的 vue没有给他生成getter setter 方法 所以dom的更新会没有或者慢了一秒半秒的,要用全局注册的方式:this.$set(elem, 'haveChecked', 1)
有时一个dom被删除 再被添上时 用this.$refs.stripOuter的方式 取高度 不能正常的取值 用this.$nextTick 也不行 这时 要用document.getElementsByClassName('checkHeader')[0]的方式
const headerHeight = document.getElementsByClassName('checkHeader')[0].clientHeight + 1
const stripHeight0 = document.getElementsByClassName('stripOuter')[0] ? document.getElementsByClassName('stripOuter')[0].clientHeight : 0
const stripHeight1 = document.getElementsByClassName('stripOuter')[1] ? document.getElementsByClassName('stripOuter')[1].clientHeight : 0
this.$refs.searchWrapper.style.maxHeight = headerHeight + stripHeight0 + stripHeight1 + 7 + 'px'
多层级的数组对象 的深拷贝方法。如果用[...arr] 的方式 只能深拷贝一层,再深的层 是浅拷贝,同理 Object.assign(target, source1, source2)也是,深拷贝可以用下面的方式:
this.quotaDataChange = [].concat(JSON.parse(JSON.stringify(this.quotaData)))
取父级页面或者兄弟页面的某个值可以用:
this.$parent   this.$children
原文地址:https://www.cnblogs.com/yangAL/p/9898806.html