Home > Article > WeChat Applet > WeChat Mini Program Use of Loops and Nested Loops
This article mainly introduces relevant information on the use summary of WeChat mini program loops and nested loops. I hope this article can help everyone. Friends in need can refer to
WeChat mini program Summary of the use of loops and nested loops
Regarding the WeChat applet, I was recently assigned to make a WeChat applet. This is my first contact with it. Generally speaking, it is not too difficult to get started.
I have a lot of feelings about the loop problem of small programs, because I have used loops and nested loops countless times to bind data to the interface.
For us in js, we obtain the data from the interface through POST or GET request and store it in the object defined in Page:
//首页话题列表 wx.request({ url: 'https://*******************', method: 'POST', data: { pageNum: 1, pageSize: 10 }, success:function(res){ that.setData({ listTop:res.data, }) } })
In the wxml file,
wx:for="{{listTop}}"
is used to loop through the data in the output object. Here we can get the subscript through {{index }} , you can also customize the subscript:
wx:for-index="index2"
When the object com exists in listTop, we can pass wx:for="{{item.com} }" to loop through the data in the loop.
In an actual project, I encountered such a problem: During the nested loop process, I need to convert the value of a certain field, such as the timestamp into date/a few days ago, etc. At this time, we should
Know that the WeChat applet does not support the interface to directly call JS. How should we solve it at this time:
At first, I spared a lot of trouble. I always wanted to use JS in JS. By looping it into an object, and then looping it out on the interface, I was actually close to the result in the previous step, but in actual development, I still have many shortcomings as a novice who has only been employed for less than a month, so You are trapped in an endless loop.
Solution: When you traverse, just replace the original data with the desired data. . . . (It’s very simple, but the authorities are confused, but since I solved it myself, there may be a better way, so this is just a description)
for (var i = 0; i < res.data.data.length;i++){ console.log(res.data.data[i].comments+"**********"+i) console.log("***"+i) if (res.data.data[i].comments !=null){ for (var j = 0; j < res.data.data[i].comments.length;j++){ res.data.data[i].comments[j].createTime=transDate(res.data.data[i].comments[j].createTime) } } }
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About WeChat Mini Program Introduction to the life cycleAbout the development of WeChat mini program canvas
##
The above is the detailed content of WeChat Mini Program Use of Loops and Nested Loops. For more information, please follow other related articles on the PHP Chinese website!