Vue指令v-for遍历输出JavaScript数组及json对象的常见方式小结
作者:Mosowe 时间:2024-05-28 15:47:20
本文实例讲述了Vue指令v-for遍历输出JavaScript数组及json对象的常见方式。分享给大家供大家参考,具体如下:
定义数据:
<script>
new Vue({
el:"#test",
data:{
message:"infor",
list:["a","b","c","d","e"],
web:{
"百度":"https://www.baidu.com",
"搜狐":"https://www.sohu.com",
"新浪":"https://www.sina.com",
"淘宝":"https://www.taobao.com"
}
}
})
</script>
html结构:
<div id="test">
<div>{{ message }}</div>
<div>{{ list }}</div>
<div>{{ web }}</div>
</div>
完整示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-for遍历</title>
</head>
<body>
<div id="test">
<div>{{ message }}</div>
<div>{{ list }}</div>
<div>{{ web }}</div>
</div>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
<script>
new Vue({
el:"#test",
data:{
message:"infor",
list:["a","b","c","d","e"],
web:{
"百度":"https://www.baidu.com",
"搜狐":"https://www.sohu.com",
"新浪":"https://www.sina.com",
"淘宝":"https://www.taobao.com"
}
}
})
</script>
</body>
</html>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
v-for对数组的几种输出方式:
1.只输出value值
html代码:
<div id="test">
<div v-for = "item in list">{{ item }}</div>
</div>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
2.输出value值且输出对应的索引值
html代码:
<div id="test">
<div v-for = "(item,index) in list">{{ item }}的索引值是{{ index }}</div>
</div>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
v-for对json格式的几种输出方式
1.只输出value值
html代码:
<div id="test">
<div v-for = "item in web">{{ item }}</div>
</div>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
2.输出value值和key值
html代码:
<div id="test">
<div v-for = "(item,key) in web">{{ key }} 的网址是 : {{ item }}</div>
</div>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
3.输出value值,key值和索引值index
html代码:
<div id="test">
<div v-for = "(item,key,index) in web">{{ index }}__{{ key }} 的网址是 : {{ item }}</div>
</div>
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.aspxhome.com/code/HtmlJsRun册数,输出结果:
可以看出,在数组里面,小括号里面的参数第一位是value值,第二位是索引值
在json里面,第一位是value值,第二位是key值,第三位是索引值
有的文章里面说$index是数组的内置变量数组下标,$key是json内置变量,可是我没有测出来,且提示有错,不知道这个到底对不对。
希望本文所述对大家vue.js程序设计有所帮助。
来源:https://blog.csdn.net/skyblacktoday/article/details/78023653
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MybatisPlus如何处理Mysql的json类型
![](https://img.aspxhome.com/file/2023/1/70591_0s.png)
深入浅出SQL之左连接、右连接和全连接
MySQL动态修改varchar长度的方法
在Python程序中实现分布式进程的教程
![](https://img.aspxhome.com/file/2023/5/76725_0s.png)
Python图片处理之图片裁剪教程
![](https://img.aspxhome.com/file/2023/3/118643_0s.jpg)
eWebEditor 辑器按钮失效 IE8下eWebEditor编辑器无法使用的解决方法
![](https://img.aspxhome.com/file/2023/4/70624_0s.gif)
详解python3中的真值测试
都2019年了,还问http中GET和POST的区别
![](https://img.aspxhome.com/file/2023/9/132289_0s.jpg)
J2EE基础应用:J2EE中SQL语句自动构造方法
Django框架实现分页显示内容的方法详解
php实现表单多按钮提交action的处理方法
Python使用Opencv打开笔记本电脑摄像头报错解问题及解决
CSS结合js实现动态更换皮肤
python3中dict(字典)的使用方法示例
mYsql日期和时间函数不求人
python正则实现提取电话功能
番茄的js表单验证类
Linux操作系统中如何安装MySQL数据库
一个导航的前端实现
![](https://img.aspxhome.com/file/UploadPic/200811/13/step1-40s.png)