为了账号安全,请及时绑定邮箱和手机立即绑定

删除数组中的元素,怎么将删除元素的id组成一个数组

删除数组中的元素,怎么将删除元素的id组成一个数组

皈依舞 2019-03-06 18:15:44
removeCost: function (item) {  let index = this.form.addReceiptsCostVOList.indexOf(item)    let ids = []  for (var i = 0; i < this.form.addReceiptsCostVOList.length; i++) {    ids.push(item.costId)    this.form.delCostIds = ids  }  if (index !== -1) {      this.form.addReceiptsCostVOList.splice(index, 1)    }      console.log('form', this.form)}想着在删除一条数据后,获取删除数据的id,组成一个数组,怎么删除多条数据,获取多个id ,怎么设置循环条件
查看完整描述

3 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

removeCost: function (item) {

  let index = this.form.addReceiptsCostVOList.indexOf(item)

  let ids = []

  for (var i = 0; i < this.form.addReceiptsCostVOList.length; i++) {

    ids.push(item.costId)

  }

  this.form.delCostIds = ids

  for (let i = 0; i < ids.length; i++) {

    this.form.addReceiptsCostVOList.splice(ids.length - 1 - i, 1)

  }

  console.log('form', this.form)

}


查看完整回答
反对 回复 2019-03-21
?
繁华开满天机

TA贡献1816条经验 获得超4个赞

可以考虑 filter 过滤掉包含 item数组里的id的元素


查看完整回答
反对 回复 2019-03-21
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

可以考虑使用闭包函数改造一下, 需要注意下this指向


removeCost: (() => {

  let ids = []

  this.form.delCostIds = ids

  return (item) => {

     let index = this.form.addReceiptsCostVOList.indexOf(item)

     if (index !== -1) {

         ids.push(item.costId)

         this.form.addReceiptsCostVOList.splice(index, 1)

     }

  }

})()


查看完整回答
反对 回复 2019-03-21
  • 3 回答
  • 0 关注
  • 782 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号