jsÖÐÔõÑù±éÀú¹¤¾ß
javascript ÖÐÓм¸ÖÖÒªÁì¿ÉÒÔ±éÀú¹¤¾ß£ºÊ¹Óà for…in Ñ»·£¬±éÀú¹¤¾ßÖпÉö¾ÙµÄÊôÐÔ£»Ê¹Óà object.keys() ÒªÁ죬·µ»Ø¹¤¾ßËùÓпÉö¾ÙÊôÐÔµÄÊý×飻ʹÓà object.values() ÒªÁ죬·µ»Ø¹¤¾ßËùÓпÉö¾ÙÊôÐÔÖµµÄÊý×飻ʹÓà object.entries() ÒªÁ죬·µ»Ø¹¤¾ßËùÓпÉö¾Ù¼üÖµ¶ÔµÄÊý×é¡£
ÔÚ JavaScript ÖбéÀú¹¤¾ß
ÔÚ JavaScript ÖУ¬¹¤¾ßÊÇÒ»ÖÖÊý¾Ý½á¹¹£¬Ëü°üÀ¨¼üÖµ¶Ô¡£±éÀú¹¤¾ß¾ÍÊÇ»á¼ûºÍ´¦ÀíÕâЩ¼üÖµ¶ÔµÄÀú³Ì¡£Óм¸ÖÖÒªÁì¿ÉÒÔ±éÀú JavaScript ¹¤¾ß£º
ʹÓà for…in Ñ»·
for…in Ñ»·±éÀú¹¤¾ßÖпÉö¾ÙµÄÊôÐÔ£º
const obj = { name: "Jane", age: 30, city: "New York" }; for (let prop in obj) { // prop ΪÊôÐÔÃû£¬obj[prop] ΪÊôÐÔÖµ console.log(prop); console.log(obj[prop]); }
µÇ¼ºó¸´ÖÆ
Êä³ö£º
name Jane age 30 city New York
µÇ¼ºó¸´ÖÆ
ʹÓà Object.keys()
Object.keys() ÒªÁì·µ»Ø¹¤¾ßËùÓпÉö¾ÙÊôÐÔµÄÊý×飺
const obj = { name: "Jane", age: 30, city: "New York" }; const keys = Object.keys(obj); for (let i = 0; i <p>Êä³ö£º</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">name Jane age 30 city New York
µÇ¼ºó¸´ÖÆ
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£