This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Wechat small program is a new way to connect users and services, it can be easily obtained and spread in wechat, at the same time with excellent use experience

Wechat small program to solve the double loop

Prepare knowledge

You can use wx:for-item to specify the name of the variable for the current element, and wx:for-index to specify the name of the variable \

Wx :key: specifies the unique identifier of an item in the list. The value of Wx :key is provided in two forms

  1. A string representing a property of item in the array of a for loop, such as:wx:key="item"
  2. Reserved keywords*thisRepresents the item itself in the for loop. This representation requires that the item itself be a unique string or number

Note: If wx:key is not provided, a warning is generated

If you know for sure that the list is static, or if you don’t care about its order, you can ignore it

WXML page:

<view>`
   <! -- Category navigation -->
   <scroll-view scroll-y="true">
       <view wx:for="{{subjectArray}}" wx:key="item">{{item.subjectName}}</view>
   </scroll-view>


   <scroll-view scroll-y="true">
       <! -- Subject Presentation -->`
       <view wx:for="{{subjectArray}}" wx:key="item">
           <view>{{item.subjectName}}</view>
           <view wx:for="{{item.professionArray}}" wx:for-item="professionitem" wx:key="{{professionitem}}">
               <text>{{professionitem.name}}</text>
           </view>
       </view>
   </scroll-view>
</view>
Copy the code

Js page:

page({
    data: {},onLoad: function(options) {
        const that = this
        // Define a set of arrays
        var array1 = [  // A one-dimensional array
            {
                name: 'design'.array2: [  // A two-dimensional array
                    {
                        name: 'Graphic Design'}, {name: 'Interior Design'}]}, {name: 'construction'.array2: [{name: 'First Class Builder'}, {name: 'Secondary Builder'}, {name: 'Fire Engineer II'}]}, {name: 'psychological'.array2: [{name: 'Counselor'}}]]var subjectArray = []
        // The first layer of the loop
        for(var idx in array1) {           
            var subject = array1[idx]
            var temp = {
                subjectName: subject.name,
                professionArray: subject.array2
            }
            subjectArray.push(temp)
        }    
        
        that.setData({
            subjectArray: subjectArray
        })
    }
})
Copy the code

(For attention)

Welcome to follow my public number: A class Coder, get daily dry goods push. Finally, thanks again for reading. I’m Silly