vue往数组中追加字段

  • 例:往数组中追加color字段
let result = res.data
let c = ['red','cyan','blue','green','orange','purplered','purple','brown','yellowgreen','grey']
for(let i = 0 ; i < result.length; i++){
	let r = Math.floor(Math.random()*10)
	this.$set(result[i],'color',c[r])
}
this.planList = result

 

  • 请求接口数据示例
this.$http.get('v1/information-category-list', {params: {}}).then(res => {
				if (res.code == 1){
					let result = res.data
					let c = ['red','cyan','blue','green','orange','purplered','purple','brown','yellowgreen','grey']
					for(let i = 0 ; i < result.length; i++){
						let r = Math.floor(Math.random()*10)
					    this.$set(result[i],'color',c[r])
					}
				    this.planList = result
				}else{
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
				}
			}).catch(err => {
				uni.showToast({
					title: err,
					icon: 'none',
					duration: 1000
				})
			})

 

© 版权声明
THE END
喜欢就支持一下吧
分享