JavaScript中遍历获取Json中属性值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> var data = { name: '小花', age: '17岁', sex: '男' }; for (var a in data) { console.log('这是属性名' + a); /*属性名*/ console.log('属性值' + data[a]); /*属性值*/ }; </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> var a = ['橡胶', '苹果', '土豆']; for (var x in a) { console.log('在数组中的位置 - '+x); /*在数组中的位置*/ console.log('值 - '+a[x]); /*值*/ }; </script> </body> </html>