{"id":77784,"date":"2024-08-31T22:13:39","date_gmt":"2024-08-31T15:13:39","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=77784"},"modified":"2024-08-31T22:13:39","modified_gmt":"2024-08-31T15:13:39","slug":"foreach-javascript-la-gi","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/","title":{"rendered":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#Tong_quan_ve_forEach_JavaScript\" >T\u1ed5ng quan v\u1ec1 forEach JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#Diem_manh_va_diem_yeu_cua_forEach_JavaScript\" >\u0110i\u1ec3m m\u1ea1nh v\u00e0 \u0111i\u1ec3m y\u1ebfu c\u1ee7a forEach JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#Mot_so_thao_tac_co_ban_voi_forEach_JavaScript_va_vi_du_chi_tiet\" >M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi forEach JavaScript v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#Cau_hoi_thuong_gap_forEach_JavaScript\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p forEach JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#Tong_ket_ve_forEach_JavaScript\" >T\u1ed5ng k\u1ebft v\u1ec1 forEach JavaScript<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach() gi\u00fap th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 ch\u1ee9c n\u0103ng c\u01a1 b\u1ea3n. \u0110\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c n\u00e0y, b\u1ea1n c\u1ea7n hi\u1ec3u r\u00f5 t\u1eebng thao t\u00e1c v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a forEach JavaScript.<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft sau \u0111\u00e2y \u0111\u1ec3 \u0111\u01b0\u1ee3c gi\u1ea3i \u0111\u00e1p chi ti\u1ebft h\u01a1n v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ed5ng quan v\u1ec1 c\u1ee7a forEach JavaScript nh\u01b0 \u0111\u1ecbnh ngh\u0129a, c\u00fa ph\u00e1p, \u0111i\u1ec3m m\u1ea1nh v\u00e0 \u0111i\u1ec3m y\u1ebfu<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n c\u1ee7a forEach<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">So s\u00e1nh s\u1ef1 kh\u00e1c nhau gi\u1eefa forEach v\u00e0 For Loop<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_quan_ve_forEach_JavaScript\"><\/span><b> T\u1ed5ng quan v\u1ec1 forEach JavaScript<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\"><strong>forEach()<\/strong> l\u00e0 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng \u0111\u1ec3 l\u1eb7p qua c\u00e1c ph\u1ea7n t\u1eed trong m\u1ed9t m\u1ea3ng (array), th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0m \u0111\u01b0\u1ee3c cung c\u1ea5p m\u1ed9t l\u1ea7n cho m\u1ed7i ph\u1ea7n t\u1eed m\u1ea3ng theo th\u1ee9 t\u1ef1 ch\u1ec9 m\u1ee5c t\u0103ng d\u1ea7n, \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 h\u00e0m callback. C\u00fa ph\u00e1p chu\u1ea9n \u0111\u1ec3 vi\u1ebft v\u00f2ng l\u1eb7p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(callbackFunction);<\/span>\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(callbackFunction, thisValue);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3, h\u00e0m callback c\u00f3 th\u1ec3 ch\u1ea5p nh\u1eadn t\u1ed1i \u0111a ba \u0111\u1ed1i s\u1ed1 kh\u00e1c nhau, m\u1eb7c d\u00f9 kh\u00f4ng ph\u1ea3i t\u1ea5t c\u1ea3 \u0111\u1ec1u b\u1eaft bu\u1ed9c.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">((currentElement) =&gt; { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(currentElement) { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">((currentElement, index) =&gt; { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(currentElement, index) { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">((currentElement, index, <\/span><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">) =&gt; { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(currentElement, index, array){ <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> })<\/span>\r\n\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">((currentElement, index, <\/span><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">) =&gt; { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> }, thisValue)<\/span>\r\n<span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\">(currentElement, index, array) { <\/span><span style=\"font-weight: 400;\">\/* ... *\/<\/span><span style=\"font-weight: 400;\"> }, thisValue)<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p tr\u00ean ch\u00ednh l\u00e0 c\u00fa ph\u00e1p chung \u0111\u1ec3 vi\u1ebft v\u00f2ng l\u1eb7p forEach t\u00f9y thu\u1ed9c v\u00e0o gi\u00e1 tr\u1ecb b\u1ea1n mu\u1ed1n s\u1eed d\u1ee5ng. C\u1ee5 th\u1ec3 nh\u01b0:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>H\u00e0m callback:<\/b><span style=\"font-weight: 400;\"> H\u00e0m ch\u1ec9 \u0111\u01b0\u1ee3c th\u1ef1c thi m\u1ed9t l\u1ea7n cho m\u1ed7i ph\u1ea7n t\u1eed v\u00e0 c\u00f3 th\u1ec3 ch\u1ea5p nh\u1eadn c\u00e1c \u0111\u1ed1i s\u1ed1.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>currentElement:<\/b><span style=\"font-weight: 400;\"> Ph\u1ea7n t\u1eed trong m\u1ea3ng \u0111\u01b0\u1ee3c x\u1eed l\u00fd t\u1ea1i th\u1eddi \u0111i\u1ec3m v\u00f2ng l\u1eb7p x\u1ea3y ra, \u0111\u00e2y l\u00e0 \u0111\u1ed1i s\u1ed1 duy nh\u1ea5t c\u1ea7n thi\u1ebft.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Index:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ed1i s\u1ed1 t\u00f9y ch\u1ecdn mang ch\u1ec9 m\u1ee5c c\u1ee7a currentElement.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Array:<\/b><span style=\"font-weight: 400;\"> M\u1ea3ng l\u00e0 \u0111\u1ed1i s\u1ed1 t\u00f9y ch\u1ecdn tr\u1ea3 v\u1ec1 m\u1ea3ng \u0111\u00e3 \u0111\u01b0\u1ee3c truy\u1ec1n cho ph\u01b0\u01a1ng th\u1ee9c forEach()<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>thisValue:<\/b><span style=\"font-weight: 400;\"> Tham s\u1ed1 t\u00f9y ch\u1ecdn ch\u1ec9 \u0111\u1ecbnh gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong h\u00e0m g\u1ecdi l\u1ea1i.<\/span><\/li>\n<\/ul>\n<blockquote><p><i><span style=\"font-weight: 400;\">Xem th\u00eam: <\/span><\/i><strong><a href=\"https:\/\/itviec.com\/blog\/javascript-la-gi\/\"><i>JavaScript l\u00e0 g\u00ec? H\u1ecdc JavaScript c\u01a1 b\u1ea3n v\u1edbi l\u1ed9 tr\u00ecnh d\u1ec5 hi\u1ec3u nh\u1ea5t<\/i><\/a><\/strong><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Diem_manh_va_diem_yeu_cua_forEach_JavaScript\"><\/span><b> \u0110i\u1ec3m m\u1ea1nh v\u00e0 \u0111i\u1ec3m y\u1ebfu c\u1ee7a forEach JavaScript<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c forEach() s\u1edf h\u1eefu m\u1ed9t s\u1ed1 \u0111i\u1ec3m m\u1ea1nh n\u1ed5i b\u1eadt nh\u01b0:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u00fac \u0111\u1ea9y ph\u1ea7n khai b\u00e1o d\u1ec5 \u0111\u1ecdc h\u01a1n, th\u1ec3 hi\u1ec7n m\u1ee5c \u0111\u00edch l\u1eb7p l\u1ea1i c\u00e1c ph\u1ea7n t\u1eed m\u00e0 kh\u00f4ng c\u1ea7n b\u1ed9 \u0111\u1ebfm v\u00f2ng l\u1eb7p r\u00f5 r\u00e0ng.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u ng\u1eafn g\u1ecdn, gi\u1ea3m c\u00e1c m\u00e3 m\u1eabu li\u00ean quan \u0111\u1ebfn v\u00f2ng l\u1eb7p for.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u00e0m callback cho ph\u00e9p b\u1ea1n th\u1ef1c hi\u1ec7n c\u00e1c h\u00e0nh \u0111\u1ed9ng tr\u00ean t\u1eebng ph\u1ea7n t\u1eed m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">M\u1eb7c d\u00f9 forEach c\u00f3 th\u1ec3 l\u00e0m cho \u0111o\u1ea1n m\u00e3 \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n nh\u01b0ng n\u00f3 c\u00f3 m\u1ed9t h\u1ea1n ch\u1ebf ch\u00ednh l\u00e0 kh\u00f4ng c\u00f3 c\u01a1 ch\u1ebf \u0111\u1ec3 d\u1eebng l\u1eb7p d\u1ef1a tr\u00ean m\u1ed9t \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3. \u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 forEach kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c t\u00ecnh hu\u1ed1ng m\u00e0 b\u1ea1n c\u1ea7n ch\u1ea5m d\u1ee9t qu\u00e1 tr\u00ecnh l\u1eb7p s\u1edbm.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Mot_so_thao_tac_co_ban_voi_forEach_JavaScript_va_vi_du_chi_tiet\"><\/span><b>M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi forEach JavaScript v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e1ch s\u1eed d\u1ee5ng currentElement<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 m\u1ed9t m\u1ea3ng th\u00f4ng tin chi ti\u1ebft v\u1ec1 nh\u00e2n s\u1ef1 bao g\u1ed3m t\u00ean, tu\u1ed5i, ti\u1ec1n l\u01b0\u01a1ng,&#8230;<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">const staffsDetails = [<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 { <\/span><span style=\"font-weight: 400;\">name:<\/span> <span style=\"font-weight: 400;\">\"Jam Josh\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">age:<\/span> <span style=\"font-weight: 400;\">44<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">salary:<\/span> <span style=\"font-weight: 400;\">4000<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">currency:<\/span> <span style=\"font-weight: 400;\">\"USD\"<\/span><span style=\"font-weight: 400;\"> },<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 { <\/span><span style=\"font-weight: 400;\">name:<\/span> <span style=\"font-weight: 400;\">\"Justina Kap\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">age:<\/span> <span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">salary:<\/span> <span style=\"font-weight: 400;\">3000<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">currency:<\/span> <span style=\"font-weight: 400;\">\"USD\"<\/span><span style=\"font-weight: 400;\"> },<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 { <\/span><span style=\"font-weight: 400;\">name:<\/span> <span style=\"font-weight: 400;\">\"Chris Colt\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">age:<\/span> <span style=\"font-weight: 400;\">37<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">salary:<\/span> <span style=\"font-weight: 400;\">3700<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">currency:<\/span> <span style=\"font-weight: 400;\">\"USD\"<\/span><span style=\"font-weight: 400;\"> },<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 { <\/span><span style=\"font-weight: 400;\">name:<\/span> <span style=\"font-weight: 400;\">\"Jane Doe\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">age:<\/span> <span style=\"font-weight: 400;\">24<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">salary:<\/span> <span style=\"font-weight: 400;\">4200<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">currency:<\/span> <span style=\"font-weight: 400;\">\"USD\"<\/span><span style=\"font-weight: 400;\"> }<\/span>\r\n<span style=\"font-weight: 400;\">];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n mu\u1ed1n hi\u1ec3n th\u1ecb t\u1ea5t c\u1ea3 c\u00e1c t\u00ean ri\u00eang l\u1ebb v\u1edbi m\u1ed9t s\u1ed1 t\u1eeb xung quanh, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c forEach() theo c\u00e1ch nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">staffsDetails.forEach((staffDetail) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 let sentence = `I am ${staffDetail.name} a staff <\/span><span style=\"font-weight: 400;\">of<\/span><span style=\"font-weight: 400;\"> Royal Suites.`;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.log(sentence);<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 hi\u1ec3n th\u1ecb:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-77812\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro.jpg\" alt=\"foreach javascript - itviec blog\" width=\"720\" height=\"168\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro.jpg 720w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro-300x70.jpg 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro-700x163.jpg 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro-200x47.jpg 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-1-vippro-100x23.jpg 100w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, b\u1ea1n c\u0169ng c\u00f3 th\u1ec3 ph\u00e2n t\u00edch c\u1ea5u tr\u00fac gi\u00e1 tr\u1ecb c\u1ee7a currentElement trong tr\u01b0\u1eddng h\u1ee3p tr\u00ean l\u00e0 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng ch\u1ee9a c\u00e1c c\u1eb7p kh\u00f3a\/gi\u00e1 tr\u1ecb:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">staffsDetails.forEach(({ name }, index) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 let sentence = `I am ${name} a staff <\/span><span style=\"font-weight: 400;\">of<\/span><span style=\"font-weight: 400;\"> Royal Suites.`;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.log(sentence);<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<h3><b>C\u00e1ch s\u1eed d\u1ee5ng \u0111\u1ed1i s\u1ed1 index trong forEach<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 l\u1ea5y ch\u1ec9 m\u1ee5c c\u1ee7a t\u1eebng m\u1ea3ng b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng \u0111\u1ed1i s\u1ed1 ch\u1ec9 m\u1ee5c ch\u01b0a x\u00e2y d\u1ef1ng theo c\u00e1ch n\u00e0y.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">staffsDetails.forEach((staffDetail, index) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">let<\/span><span style=\"font-weight: 400;\"> sentence = <\/span><span style=\"font-weight: 400;\">`index ${index} : I am ${staffDetail.name} a staff of Royal Suites.`<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.log(sentence);<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 hi\u1ec3n th\u1ecb:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-77810\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro.jpg\" alt=\"foreach javascript - itviec blog\" width=\"744\" height=\"157\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro.jpg 744w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro-300x63.jpg 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro-700x148.jpg 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro-200x42.jpg 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-2-vippro-100x21.jpg 100w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/p>\n<h3><b>C\u00e1ch s\u1eed d\u1ee5ng m\u1ea3ng (Array)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed1i s\u1ed1 m\u1ea3ng l\u00e0 \u0111\u1ed1i s\u1ed1 th\u1ee9 ba ch\u1ee9a m\u1ea3ng g\u1ed1c \u0111ang \u0111\u01b0\u1ee3c l\u1eb7p l\u1ea1i. V\u00ec v\u1eady, b\u1ea1n c\u00f3 th\u1ec3 th\u1eed hi\u1ec3n th\u1ecb gi\u00e1 tr\u1ecb trong b\u1ea3ng theo c\u00e1ch:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">staffsDetails.forEach((staffDetail, <\/span><span style=\"font-weight: 400;\">index<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 console.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">L\u00fac n\u00e0y, k\u1ebft qu\u1ea3 s\u1ebd hi\u1ec3n th\u1ecb to\u00e0n b\u1ed9 4 m\u1ea3ng v\u00ec b\u1ea1n th\u1ef1c hi\u1ec7n l\u1ec7nh 4 m\u1ee5c v\u00e0 v\u00f2ng l\u1eb7p ch\u1ea1y 4 l\u1ea7n. Ti\u1ebfp theo, th\u00eam m\u1ed9t s\u1ed1 gi\u00e1 tr\u1ecb \u0111\u1ec3 c\u00f3 k\u1ebft qu\u1ea3 hi\u1ec3n th\u1ecb \u0111\u1ea7u ra nh\u01b0:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let <\/span><span style=\"font-weight: 400;\">scores <\/span><span style=\"font-weight: 400;\">= [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">55<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">70<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">scores.forEach((score, <\/span><span style=\"font-weight: 400;\">index, array) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 console.log(array)<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">})<\/span><span style=\"font-weight: 400;\">;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 hi\u1ec3n th\u1ecb:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-77811\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro.jpg\" alt=\"foreach javascript - itviec blog\" width=\"1033\" height=\"163\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro.jpg 1033w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro-300x47.jpg 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro-700x110.jpg 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro-200x32.jpg 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/foreach-javascript-3-vippro-100x16.jpg 100w\" sizes=\"auto, (max-width: 1033px) 100vw, 1033px\" \/><\/p>\n<h3><b>C\u00e1ch th\u00eam c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1 (number) v\u1edbi forEach<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 m\u1ed9t m\u1ea3ng \u0111i\u1ec3m s\u1ed1. B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c m\u1ea3ng forEach() \u0111\u1ec3 l\u1eb7p l\u1ea1i v\u00e0 c\u1ed9ng c\u00e1c s\u1ed1 n\u00e0y.\u00a0<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">const<\/span><span style=\"font-weight: 400;\"> scores = [12, 55, 70, 47];<\/span>\r\n\r\n<span style=\"font-weight: 400;\">let <\/span><span style=\"font-weight: 400;\">total<\/span><span style=\"font-weight: 400;\"> = 0;<\/span>\r\n<span style=\"font-weight: 400;\">scores.<\/span><span style=\"font-weight: 400;\">forEach<\/span><span style=\"font-weight: 400;\">((<\/span><span style=\"font-weight: 400;\">score<\/span><span style=\"font-weight: 400;\">) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">total<\/span><span style=\"font-weight: 400;\"> += <\/span><span style=\"font-weight: 400;\">score<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span>\r\n\r\n<span style=\"font-weight: 400;\">console.<\/span><span style=\"font-weight: 400;\">log<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">total<\/span><span style=\"font-weight: 400;\">);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ti\u1ebfp theo, b\u1ea1n ti\u1ebfn h\u00e0nh c\u1ed9ng th\u1eed t\u1ea5t c\u1ea3 m\u1ee9c l\u01b0\u01a1ng c\u1ee7a nh\u00e2n vi\u00ean l\u1ea1i v\u1edbi nhau \u0111\u1ec3 xem ch\u00fang ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let totalSalary = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">;<\/span>\r\n<span style=\"font-weight: 400;\">staffsDetails.forEach(({salary}) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 totalSalary += salary;<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span>\r\n\r\n<span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.log(totalSalary + <\/span><span style=\"font-weight: 400;\">\" USD\"<\/span><span style=\"font-weight: 400;\">); <\/span><span style=\"font-weight: 400;\">\/\/<\/span> <span style=\"font-weight: 400;\">\"14900 USD\"<\/span><\/pre>\n<h3><b>C\u00e1ch s\u1eed d\u1ee5ng callback trong forEach()<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi l\u1eb7p qua c\u00e1c m\u1ea3ng, b\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra c\u00e1c \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3 v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n v\u1edbi ph\u01b0\u01a1ng th\u1ee9c v\u00f2ng l\u1eb7p for. B\u1ea1n c\u00f3 th\u1ec3 truy\u1ec1n c\u00e1c \u0111i\u1ec1u ki\u1ec7n n\u00e0y v\u00e0o h\u00e0m callback ho\u1eb7c c\u00e1c thao t\u00e1c kh\u00e1c m\u00e0 b\u1ea1n mu\u1ed1n ch\u1ea1y tr\u00ean m\u1ed7i m\u1ea3ng.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, n\u1ebfu b\u1ea1n ch\u1ec9 mu\u1ed1n hi\u1ec3n th\u1ecb t\u00ean nh\u1eefng nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng 4000 t\u1eeb m\u1ea3ng th\u00f4ng tin chi ti\u1ebft v\u1ec1 nh\u00e2n s\u1ef1 \u0111\u00e3 khai b\u00e1o tr\u01b0\u1edbc \u0111\u00f3. B\u1ea1n c\u00f3 th\u1ec3 l\u00e0m nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">staffsDetails.forEach(({name, salary}) =&gt; {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\">(salary &gt;= <\/span><span style=\"font-weight: 400;\">4000<\/span><span style=\"font-weight: 400;\">){<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">console<\/span><span style=\"font-weight: 400;\">.log(name);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 }<\/span>\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Khi tr\u1ea3 k\u1ebft qu\u1ea3, tr\u00ecnh duy\u1ec7t s\u1ebd hi\u1ec3n th\u1ecb t\u00ean c\u1ee7a nh\u1eefng nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng l\u1edbn h\u01a1n 4000.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_forEach_JavaScript\"><\/span><b> C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p forEach JavaScript<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng forEach JavaScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng forEach JavaScript nh\u01b0:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n ch\u1ec9 mu\u1ed1n th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0nh \u0111\u1ed9ng tr\u00ean t\u1eebng ph\u1ea7n t\u1eed m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o ra m\u1ed9t m\u1ea3ng m\u1edbi, forEach s\u1ebd l\u00e0 l\u1ef1a ch\u1ecdn t\u1ed1t nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ho\u1eb7c khi b\u1ea1n kh\u00f4ng c\u1ea7n tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb m\u1edbi trong m\u1ea3ng. Do b\u1ea3n ch\u1ea5t c\u1ee7a forEach l\u00e0 th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0nh \u0111\u1ed9ng n\u00e0o \u0111\u00f3 tr\u00ean m\u1ed7i ph\u1ea7n t\u1eed m\u00e0 kh\u00f4ng c\u00f3 \u00fd \u0111\u1ecbnh tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb.<\/span><\/li>\n<\/ul>\n<h3><b>S\u1ef1 kh\u00e1c nhau gi\u1eefa forEach v\u00e0 JavaScript for loop<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 s\u1ef1 kh\u00e1c nhau gi\u1eefa forEach v\u00e0 for Loop nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>For loop<\/b><\/td>\n<td><b>forEach Loop<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">M\u1ed9t trong nh\u1eefng c\u00e1ch ban \u0111\u1ea7u \u0111\u1ec3 l\u1eb7p qua m\u1ed9t m\u1ea3ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e1ch m\u1edbi h\u01a1n v\u1edbi \u00edt m\u00e3 h\u01a1n \u0111\u1ec3 t\u1ea1o l\u1eb7p qua m\u1ed9t m\u1ea3ng.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Hi\u1ec7u su\u1ea5t nhanh h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1eadm h\u01a1n v\u00f2ng l\u1eb7p truy\u1ec1n th\u1ed1ng v\u1ec1 hi\u1ec7u su\u1ea5t<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh break c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 tho\u00e1t kh\u1ecfi v\u00f2ng l\u1eb7p<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh break kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng v\u00ec h\u00e0m callback.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Tham s\u1ed1 iterator, counter v\u00e0 increment<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e1c tham s\u1ed1 nh\u01b0 iterator, index c\u1ee7a item v\u00e0 m\u1ea3ng \u0111\u1ec3 l\u1eb7p l\u1ea1i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng t\u1eeb v\u1edbi t\u1eeb kh\u00f3a await.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a await kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng do h\u00e0m callback, c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn \u0111\u1ea7u ra kh\u00f4ng ch\u00ednh x\u00e1c.\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>forEach v\u00e0 Map kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong qu\u00e1 tr\u00ecnh l\u1eadp tr\u00ecnh, n\u1ebfu b\u1ea1n mu\u1ed1n t\u1ea1o ra m\u1ed9t m\u1ea3ng m\u1edbi v\u1edbi c\u00e1c gi\u00e1 tr\u1ecb \u0111\u00e3 \u0111\u01b0\u1ee3c chuy\u1ec3n \u0111\u1ed5i t\u1eeb m\u1ea3ng g\u1ed1c th\u00ec h\u00e3y l\u1ef1a ch\u1ecdn map(). N\u1ebfu b\u1ea1n ch\u1ec9 mu\u1ed1n l\u1eb7p qua m\u1ea3ng v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c ph\u1ee5 (nh\u01b0 in ra console ho\u1eb7c thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a t\u1eebng ph\u1ea7n t\u1eed m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o m\u1ea3ng m\u1edbi), forEach() c\u00f3 th\u1ec3 l\u00e0 l\u1ef1a ch\u1ecdn t\u1ed1t h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 \u0111i\u1ec3m kh\u00e1c bi\u1ec7t gi\u1eefa hai ph\u01b0\u01a1ng th\u1ee9c n\u00e0y nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>forEach()<\/b><\/td>\n<td><b>map()<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng tr\u1ea3 v\u1ec1 m\u1ed9t m\u1ea3ng m\u1edbi d\u1ef1a tr\u00ean m\u1ea3ng \u0111\u00e3 cho.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 m\u1ed9t m\u1ea3ng ho\u00e0n to\u00e0n m\u1edbi.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 \u201cundefined\u201d.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 m\u1ea3ng m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o theo h\u00e0m callback \u0111\u01b0\u1ee3c cung c\u1ea5p.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng tr\u1ea3 v\u1ec1 b\u1ea5t c\u1ee9 th\u1ee9 g\u00ec, do b\u1ea3n ch\u1ea5t c\u1ee7a n\u00f3 l\u00e0 th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0nh \u0111\u1ed9ng n\u00e0o \u0111\u00f3 tr\u00ean m\u1ed7i ph\u1ea7n t\u1eed m\u00e0 kh\u00f4ng c\u00f3 \u00fd \u0111\u1ecbnh tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 n\u1ed1i chu\u1ed7i c\u00e1c ph\u01b0\u01a1ng th\u1ee9c kh\u00e1c nh\u01b0 reduce(), sort(),&#8230;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ef1c hi\u1ec7n \u0111\u01b0\u1ee3c \u0111\u1ed1i v\u1edbi c\u00e1c ph\u1ea7n t\u1eed r\u1ed7ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng thay \u0111\u1ed5i m\u1ea3ng g\u1ed1c.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_ve_forEach_JavaScript\"><\/span><b>T\u1ed5ng k\u1ebft v\u1ec1 forEach JavaScript<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady, b\u00e0i vi\u1ebft \u0111\u00e3 gi\u1edbi thi\u1ec7u v\u1ec1 ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a M\u1ea3ng (Array) forEach(), cho ph\u00e9p b\u1ea1n c\u00f3 th\u1ec3 l\u1eb7p qua m\u1ed9t m\u1ea3ng c\u1ee7a b\u1ea5t k\u1ef3 m\u1ee5c n\u00e0o. <\/span><b>forEach JavaScript<\/b><span style=\"font-weight: 400;\">\u00a0c\u0169ng cho ph\u00e9p b\u1ea1n vi\u1ebft m\u00e3 m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng h\u01a1n, c\u1ea5u tr\u00fac d\u1ec5 \u0111\u1ecdc v\u00e0 \u0111\u01a1n gi\u1ea3n so v\u1edbi v\u00f2ng l\u1eb7p for.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 n\u1eafm v\u1eefng h\u01a1n n\u1ec1n t\u1ea3ng ki\u1ebfn th\u1ee9c v\u1ec1 JavaScript, h\u00e3y th\u1eed \u00e1p d\u1ee5ng ch\u00fang v\u00e0o nh\u1eefng d\u1ef1 \u00e1n th\u1ef1c t\u1ebf c\u0169ng nh\u01b0 c\u00f3 c\u01a1 h\u1ed9i t\u00ecm hi\u1ec3u nhi\u1ec1u h\u01a1n v\u1ec1 kh\u00e1i ni\u1ec7m n\u00e2ng cao. B\u1eb1ng c\u00e1ch n\u00e0y, b\u1ea1n s\u1ebd \u0111\u1ea1t \u0111\u01b0\u1ee3c nhi\u1ec1u c\u01a1 h\u1ed9i c\u0169ng nh\u01b0 th\u00e0nh t\u00edch n\u1ed5i b\u1eadt trong c\u00f4ng vi\u1ec7c, tr\u1edf th\u00e0nh Javascript Developer chuy\u00ean nghi\u1ec7p.<\/span><\/p>\n<blockquote><p><i>Xem th\u00eam:<\/i><strong> <a href=\"https:\/\/itviec.com\/blog\/javascript-developer\/\"><i>JavaScript Developer l\u00e0 g\u00ec? C\u00e1c h\u01b0\u1edbng ph\u00e1t tri\u1ec3n c\u1ee7a JavaScript Developer<\/i><\/a><\/strong><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach() gi\u00fap th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 ch\u1ee9c n\u0103ng c\u01a1 b\u1ea3n. \u0110\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c n\u00e0y, b\u1ea1n c\u1ea7n hi\u1ec3u r\u00f5 t\u1eebng thao t\u00e1c v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a forEach JavaScript. \u0110\u1ecdc b\u00e0i vi\u1ebft sau \u0111\u00e2y \u0111\u1ec3 \u0111\u01b0\u1ee3c gi\u1ea3i \u0111\u00e1p chi ti\u1ebft h\u01a1n v\u1ec1: T\u1ed5ng quan v\u1ec1 [&hellip;]<\/p>\n","protected":false},"author":207,"featured_media":77813,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-77784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft - ITviec Blog<\/title>\n<meta name=\"description\" content=\"C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach(). B\u00e0i vi\u1ebft gi\u1edbi thi\u1ec7u thao t\u00e1c v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a forEach JavaScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft\" \/>\n<meta property=\"og:description\" content=\"C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach() gi\u00fap th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 ch\u1ee9c n\u0103ng c\u01a1 b\u1ea3n. \u0110\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c n\u00e0y, b\u1ea1n c\u1ea7n hi\u1ec3u r\u00f5 t\u1eebng thao\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-31T15:13:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Uyen Ngo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Uyen Ngo\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft - ITviec Blog","description":"C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach(). B\u00e0i vi\u1ebft gi\u1edbi thi\u1ec7u thao t\u00e1c v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a forEach JavaScript.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/","og_locale":"vi_VN","og_type":"article","og_title":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft","og_description":"C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach() gi\u00fap th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 ch\u1ee9c n\u0103ng c\u01a1 b\u1ea3n. \u0110\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c n\u00e0y, b\u1ea1n c\u1ea7n hi\u1ec3u r\u00f5 t\u1eebng thao","og_url":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-08-31T15:13:39+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg","type":"image\/jpeg"}],"author":"Uyen Ngo","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Uyen Ngo","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"9 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/"},"author":{"name":"Uyen Ngo","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/f4cd1226846e0258c664e170d3e52d20"},"headline":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft","datePublished":"2024-08-31T15:13:39+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/"},"wordCount":2070,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/","url":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/","name":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg","datePublished":"2024-08-31T15:13:39+00:00","description":"C\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c l\u1eb7p trong JavaScript, bao g\u1ed3m forEach(). B\u00e0i vi\u1ebft gi\u1edbi thi\u1ec7u thao t\u00e1c v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a forEach JavaScript.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/08\/forEach-Javascript-vippro.jpg","width":1500,"height":790},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/foreach-javascript-la-gi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"forEach JavaScript: M\u1ed9t s\u1ed1 thao t\u00e1c c\u01a1 b\u1ea3n v\u00e0 v\u00ed d\u1ee5 chi ti\u1ebft"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/f4cd1226846e0258c664e170d3e52d20","name":"Uyen Ngo","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","caption":"Uyen Ngo"},"url":"https:\/\/itviec.com\/blog\/author\/uyen-ngo\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/77784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/207"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=77784"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/77784\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/77813"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=77784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=77784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=77784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}