【过滤器filter】通过id关联的两数组,数据不匹配的数组输出问题の解决方案

【问题现象]
Vue中使用v-for遍历数组时可能会出现两个不同数组间的关联问题,在data中定义了商品列表brandListdepList

data: {
	// 品牌列表数据
	brandList: [
		{id: 1, name: '宝马', status: 'true', depId: '101', time: new Date()},
		{id: 2, name: '奔驰', status: 'true', depId: '102', time: new Date()},
		{id: 3, name: '大众', status: 'true', depId: '103', time: new Date()},
	],
	depList: [
		{id: 101, depName: '厂家1'},
		{id: 102, depName: '厂家2'},
	]
},

使用v-for循环遍历输出时,表格中默认呈现的时没有经过匹配的brandList中的depId,当加上v-if的筛选条件后,在遍历时会出现品牌名称为大众的品牌的厂家没有对应的厂家,导致表格展现异常。
【解决方案】
配置过滤器,使用过滤器对brandList中的depId进行转换

  1. 定义全局变量that,便于过滤器选择depList
let that
  1. beforeCreate生命周期将this赋值给that,通过that获得depList
beforeCreate: function () {
	that = this
},
  1. 配置过滤器,对brandList中的depId进行转换
depChange(depId){
	let depName = '-'
	for(let i = 0; i < that.depList.length; i++){
		if(depId == that.depList[i].id)
			depName = that.depList[i].depName
		console.log(depName)
	}
	return depName
}
  1. 输出生产厂家时,使用过滤器进行输出
<td>
	{{brand.depId | depChange}}
</td>

【最终结果]
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值