Home > Article > WeChat Applet > Detailed explanation of WeChat applet loops and nested loops
This article mainly introduces relevant information on the use summary of WeChat applet loops and nested loops. I hope this article can help everyone. Friends in need can refer to it. I hope it can help everyone.
For us to obtain data from the interface through POST or GET requests in js 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, pass
wx:for="{{listTop}}"
To realize the loop output of data in the object, here we can get the subscript through {{index }}, or we can customize the subscript:
wx:for-index="index2"
When the object com exists in listTop, we can use 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)
}
}
}
Related recommendations:
WeChat development list rendering multi-layer nested loop
Oracle nested loop execution plan optimization
Multiple foreach nesting Loop problem
The above is the detailed content of Detailed explanation of WeChat applet loops and nested loops. For more information, please follow other related articles on the PHP Chinese website!