为什么for in 没有把对象里所有属性遍历出来?

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><div id="container"></div><script>           (function(){           var data = [{num:20,age:30},{num:30},{num:40},{num:50}];           var container = document.getElementById('container');           function render(data){               container.innerHTML = data.map(function(item){                   var i ;                   console.log(item)                   for(i in item){                       console.log(i)                       if(item.hasOwnProperty(i)){                           // console.log(i);                           return item[i];                       }                                          }               });                          }                      render(data);       })()   </script></body></html>


江城fen
浏览 1365回答 1
1回答

阿尔法小队长

因为你遍历到第一个num,就直接return出去了,跳出循环,不会去继续遍历后面的了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript