1.数组遍历

  1. var arr = ['red','green','blue'];
  2. for (var i=0;i<arr.length;i++){
  3. console.log(arr[i]);
  4. }

2.对象遍历

  1. //第一种
  2. const obj = {
  3. id: 1,
  4. name: "zhangsan",
  5. age: 18,
  6. };
  7. for (let key in obj) {
  8. console.log(key + "---" + obj[key]);
  9. }
  10. //第二种
  11. const obj1 = {
  12. id: 1,
  13. name: "zhangsan",
  14. age: 18,
  15. };
  16. console.log(Object.keys(obj1));
  17. console.log(Object.values(obj1));
  18. //第三种
  19. const obj2 = {
  20. id: 1,
  21. name: "zhangsan",
  22. age: 18,
  23. };
  24. Object.getOwnPropertyNames(obj2).forEach(function (key) {
  25. console.log(key + "---" + obj2[key]);
  26. });