天天看点

微信小程序给循环数组动态添加元素,评论或点赞数量超过999,展示999+

有时候为了展示,需要往每条数组中添加一个值为boolen类型的元素;

首先要循环这个数组,其次添加元素,然后定义元素的boolen值

代码如下:

wxml代码:

<view wx:for="{{arr}}" wx:key="{{item.index}}" class='list'>
  <view wx:if = "{{item.show}}" class='listChild'>999+</view>
  <view wx:else>{{item.num}}</view>
</view>
           

wxcss代码:

.list{
  width: 100%;
  margin: 0 auto;
  border: 1rpx solid #eee;
  color: #3492b7;
  text-align: center;
}
           

js代码:

Page({
  data: {
    arr:[
      { "num": "666" },
      { "num": "888" },
      { "num": "555" },
      { "num": "999" },
      { "num": "1024" }
    ]
  },

  onShow:function(){
    const a = this.data.arr;
     const b = a.map(function (el) {
       el.newel = 'show';
       el.show = false
       var number = el.num = el.num
       if (number > 999) {
         //数值超过999的显示999+
        el.show = true
      }
      console.log(a)
      return (el);
      
    })
  this.setData({
    arr:a
  })
  }
})