关于小程序js遍历数组问题

关于小程序js遍历数组问题,第1张

<html>

<head>

<title>无标题</title>

</head>

<body>

<script type="text/javascript">

var ac = ['id1','id2']

var ad = ['title1','title2']

var ae = ['uid1','uid2']

var af = ['utitle1'消薯,'utitle2']

var r=[]

for(var i=0i<ac.lengthi++)

r.push({id:ac[i],title:ad[i],ulist:[{uid:ae[i],utitle:af[i]}]})

//输出,以进行核对

document.write("[")

for(var i=0i<r.lengthi++)

{

document.write("{")

document.write("id:'"+r[i]['id']+"',")

document.write("title:'"+r[i]['title']+"',")

document.write("ulist:[{uid:'"+r[i]['ulist'][0]['uid']+"销桥猜',")

document.write("utitle:'"+r[i]['ulist'][0]['utitle']+"'}]")

document.write("}")

if(i!=r.length-1)

document.write(",<br />")

}

document.write("]")

<亏型/script>

</body>

</html>

1、在MYSQL中新建一个test数据库,在里面新建一张type数据表,下面将从该表获得数据。

2、新建一个project小程序项目。

3、在project项目内,新建一个index模块来从数据库获得数据,并将数据输出在首拍index.wxml页面

4、在index模块中index.js页面,在onLoad中使用wx.request方法向服务端文件请求数据。url为请求数据的地址;data为传输的参数,这里设置为空;method为传递参数的方式,header用于设置请求的数据类型,success是成功获得数据后的 *** 作。

5、在index.wxml页面,使用for循环遍历输出index.js传递过来的唯物数据data,通过item将data数据输出在页面。

6、在服务端新建一个api.php页面来接收微信小程序的数据请求。

7、最后编译微信小程序,查看从数据表type获者山羡得数据内容,成功从数据库读取数据并输出在页面上。

<block wx:for="{{XXList}}" wx:for-item="listItem">

这个是需要在你的page页面的js里面有XXList这友谨个数值变量

可以从小程序开发者论坛好瞎基神轮什么的找找demo看下


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12436144.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存