在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 小程序返回的數(shù)據(jù)如何在swiper做分組滑動

小程序返回的數(shù)據(jù)如何在swiper做分組滑動

接口:/api/users
返回:data:[{id: "1", title: "小明", img: "xxx.com/1.jpg"}, {id: "2", title: "小明2", img: "xxx.com/1.jpg"},{id: "3", title: "小明3", img: "xxx.com/3.jpg"}]
數(shù)據(jù)大概18條,我只打了3條

分組放到小程序的swiper里的 swiper-item做頁面切換

9條數(shù)據(jù)為一組, 把數(shù)據(jù)放到 swiper-item
<block wx:for="{{data}}"><swiper-item><view>{{item.title}}</view></swiper-item></block>

怎么樣%9循環(huán)出兩個swiper-item呢?

回答
編輯回答
吢涼

解決

      const test = [1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
      const func = function (arry, length) {
        let result = []
        arry.forEach((ele, index) => {
          const i = Math.floor(index / length)
          console.log(i)
          if (!result[i]) {
            result[i] = []
          }
        })
        return result
      }
      console.log(func(test, 5))

原理

就是除法運算,根據(jù)需要的長度來組合二維數(shù)組
有點類似于九九乘法表

2017年1月17日 14:58
編輯回答
影魅

自己把數(shù)據(jù)封裝好再循環(huán)就好啦

const test = [1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
const i = Math.floor(index / length);
let result = []
result[0]= test.slice(0,i-1);
result[1]= test.slice(i-1,test.length-1);
//最后拿resul循環(huán)即可
2017年7月11日 04:07