for in 和 for of 的区别和v-for指令的三种使用方法

for...in 循环:只能获得对象的键名,不能获得键值
for...of 循环:允许遍历获得键值

var arr = ['red', 'green', 'blue']

for(let item in arr) {
console.log('for in item', item)
}
/*
for in item 0
for in item 1
for in item 2
*/

for(let item of arr) {
console.log('for of item', item)
}
/*
for of item red
for of item green
for of item blue
*/

总之,for...in 循环主要是为了遍历对象而生,不适用于遍历数组

for...of 循环可以用来遍历数组、类数组对象,字符串、Set、Map 以及 Generator 对象

转自:https://www.cnblogs.com/rogerwu/p/10738776.html

v-for指令的三种使用方法

1.迭代数组

id:{{item.id}}---名字:{{item.name}}---索引{{item.age}}

2.迭代对象中的属性

{{val}}--{{key}}--{{i}}


3.迭代数字

这是第{{i}}个标签}}


在组件中使用v-for 循环的时候,如果有问题,必须使用key指定
https://www.cnblogs.com/guomouren/p/12655638.html

posted @ 2021-12-11 17:10  方寸间  阅读(736)  评论(0)    收藏  举报