数组和字符串删除和添加

81 阅读1分钟

数组增加

const idx = this.data.findIndex(d => d === day.id)

if (

idx >= 0 ||

new Date(day.id) < new Date(moment().format('YYYY-MM-DD'))

) {

// this.data.splice(idx, 1)

} else {

  this.data.push(day.id)

// this.$emit('change', JSON.stringify(this.data))

 }

字符串增加

const idx = this.data.indexOf(day.id)

      if (          idx >= 0 ||          new Date(day.id) < new Date(moment().format('YYYY-MM-DD'))        ) {          // this.data.splice(idx, 1)        } else {          // this.data.push(day.id)          if (this.data == '') {            this.data = day.id          } else {            this.data = this.data + ',' + day.id          }          // this.$emit('change', JSON.stringify(this.data))          this.$emit('change', this.data)        }

数组删除

 this.data = this.data.filter(d => d !== date)

// this.$emit('change', JSON.stringify(this.data))

字符串删除

 this.data = this.data

.split(',')

.filter(d => d !== date)

.join(',')

// this.$emit('change', this.data)

watch数据改变

watch: {    value: {      handler(v) {        if (v) {          // this.data = JSON.parse(v)          this.data = v        } else {          // this.data = []          this.data = ''        }      },      immediate: true    }
}