8

javascript中的列表与对象遍历

 3 years ago
source link: https://www.yangyanxing.com/article/array_and_object_in_javacript.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

javascript中的列表与对象遍历

工作中不是经常会写javascript,所以每次遇到写js代码的时候,都要上网上各种查,这里先总结一下,经常用的,之后再慢慢添加。

列表的遍历

1
2
3
4
5
6
7
8
9
10
var names = ["杨彦星1","杨彦星2","杨彦星3"]

for(var i in names){
console.log(i,names[i])
}

---
0 杨彦星1
1 杨彦星2
2 杨彦星3

i 是列表中的顺序,从0 开始,且是string,不是int

对象的遍历

1
2
3
4
5
6
7
8
var persion = {"name":"yyx","age":18,"sex":"male"}
for(var k in persion){
console.log(k,persion[k])
}
---
name yyx
VM870:2 age 18
VM870:2 sex male

本文由 杨彦星 发表于 杨彦星的个人博客 ,采用「署名 4.0 国际」创作共享协议。
非商业转载请注明作者及出处。商业转载请联系作者本人。
本文标题为: javascript中的列表与对象遍历

本文链接为:https://www.yangyanxing.com/article/array_and_object_in_javacript.html

欢迎关注我的公众号--序语程言,里面会分享一些编程的基础与一些问题的解决

qrcode.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK