遍历数组并且添加键值对

8 篇文章 0 订阅

实例:根据接口渲染列表,并且实现点击右边小图标实现是否展示功能

1.HTML代码(problemList 是item)

<template>
	<view class="container">
		<view class="frequelist" >
            //problemList相当于item,指的是数组中的每一项
			<view class="frequblock" v-for="(problemList,key,index) in frearry">
				<view class="freblock">
					<view class="frequestion">
						<image class="frequestionlogo" src="../../static/images/frequestionlogo.png" mode=""></image>
						<view class="questionnumber">Q{{problemList.id}}</view>
					</view>
					<view class="questionspecific">{{problemList.question}}</view>
					<view class="" @click="showTextImage(index)" >
						<image class="frequestionmoreup" v-if="problemList.showText" src="../../static/images/frequestionmoreup.png" mode=""></image>
						<image class="frequestionmoredown" v-else src="../../static/images/frequestionmoredown.png" mode=""></image>
					</view>
				</view>
				<view class="answercontent" v-if="problemList.showText">{{problemList.answer}}</view>
				<view class="greyblockline"></view>
			</view>
			<view class="frewhitblock"></view>
		</view>
	</view>
</template>

2.调接口代码

<script>
//引用接口
import { getfrequentlyList } from "@/api/otherApi.js"
	export default {
		data() {
			return {
				frearry:[],
				showText:"",
			}
		},
		onLoad(){
            //进入页面就渲染
			this.getgoods()
		},
		methods: {
			getgoods(){
				let obj = {}
                //选中接口数组
				let freadetemine=this.frearry
				let selfThis = this
                //调用接口
				getfrequentlyList(obj).then(res=>{
					let goodsManage=res.data	
                    //遍历各项数组并添加			
					for (let problemList of goodsManage.data) {
						 problemList.showText=false
					}				
                     //添加数据之后再渲染到页面上(注意前后顺序)	
					selfThis.frearry=goodsManage.data
                    //这里可以打印页面数据检查是否添加
					console.log(goodsManage.data)
				  
				}).catch(err=>{
				  console.log('ERROR')
				});
			
			},
            //点击事件(决定是否展示)
			showTextImage(index){
				let frecontnum=this.frearry
				frecontnum[index].showText=!frecontnum[index].showText
			}
			
		}
	}
</script>

如有错误,请各位大佬不吝赐教~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值