How does the data returned by Mini Program do grouping sliding in swiper?

API: / api/users
return: data: [{id: "1", title: "Xiaoming", img: "xxx.com/1.jpg"}, {id: "2", title: "Xiaoming 2", img: "xxx.com/1.jpg"}, {id: "3", title: "Xiaoming 3", img: "xxx.com/3.jpg"}]
data is about 18, but I only typed 3

.

put the swiper-item in Mini Program"s swiper to switch pages

9 pieces of data are grouped into a group. Put the data in swiper-item
< block wx:for= "{{data}}" > < swiper-item > < view > {{item.title}} < / view > < / swiper-item > < / block >

how about% 9 loop out two swiper-item?

Mar.02,2021

just encapsulate the data and recycle it yourself

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

solve

      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))

principle

is the division operation, combining two-dimensional arrays according to the required length
is somewhat similar to the ninety-nine multiplication table

Menu