最近在折腾微信小程序,遇到这么一个情况:后端返回一个key-value的对象数据,需要遍历对象的key-value,然后渲染到视图中。就像下面这样:
data: {
baseUrl: global.baseUrl,
anonymous: false,
commentC_: ['有耐心', '准时守约', '服务态度好', '接单快', '人长得帅'],
comment: {
patient: true,
onTime: true,
service: true,
fast: true,
handsome: true,
},
},
html:
<view class="mEBCommentItem" wx:for="{{comment}}" wx:for-index="key" wx:for-item="value">
<view data-index="{{key}}" bindtap="selectComment">{{value}}</view>
</view>
当wx:for遍历对象时,wx:for-index会得到对象的key,wx:for-item会得到对象的value
微信小程序文档没有提到遍历对象,以上代码在微信开发者工具V1.02上测试通过。