<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Giới thiệu về mảng hai chiều trong JavaScript</title>
<meta name="description" content="Giới thiệu về mảng hai chiều trong JavaScript - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;mangvn.org&#x002F;savefile&#x002F;Thu-thuat-tin-hoc&#x002F;Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html">
<meta name="author" content="MangVN">
<meta name="copyright" content="MangVN [admin@vinades.vn]">
<meta name="generator" content="NukeViet v4.5">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Giới thiệu về mảng hai chiều trong JavaScript">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;mangvn.org&#x002F;savefile&#x002F;Thu-thuat-tin-hoc&#x002F;Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html">
<meta property="og:site_name" content="MangVN">
<meta property="og:url" content="https://mangvn.org/savefile/Thu-thuat-tin-hoc/Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html">
<link rel="shortcut icon" href="https://mangvn.org/favicon.ico">
<link rel="canonical" href="https://mangvn.org/savefile/Thu-thuat-tin-hoc/Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html">
<link rel="alternate" href="https://mangvn.org/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Tin-nhanh-Cong-nghe/" title="Tin Tức - Tin nhanh Công nghệ" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Virut-Hacker-Bao-mat/" title="Tin Tức - Virut, Hacker & Bảo mật" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/CNTT/" title="Tin Tức - CNTT" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Vien-thong/" title="Tin Tức - Viễn thông" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/May-tinh/" title="Tin Tức - Máy tính" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Dien-thoai/" title="Tin Tức - Điện thoại" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/May-anh/" title="Tin Tức - Máy ảnh" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Thu-thuat-Phan-mem/" title="Tin Tức - Thủ thuật - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Phan-mem-Tin-hoc/" title="Tin Tức - Phần mềm Tin học" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Thu-thuat-tin-hoc/" title="Tin Tức - Thủ thuật tin học" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Thiet-ke-Web/" title="Tin Tức - Thiết kế Web" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Kien-thuc-co-ban/" title="Tin Tức - Kiến thức cơ bản" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/PHP-HTML-JavaScript/" title="Tin Tức - PHP, HTML & JavaScript" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Quang-cao-tang-thu-nhap/" title="Tin Tức - Quảng cáo tăng thu nhập" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Tao-Web-bang-NukeViet/" title="Tin Tức - Tạo Web NukeViet" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Ban-tin-an-ninh/" title="Tin Tức - Bản tin an ninh" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Tin-tuc-NukeViet/" title="Tin Tức - Tin tức NukeViet" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Nhan-vat-su-kien/" title="Tin Tức - Nhân vật & sự kiện" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Huong-dan-su-dung/" title="Tin Tức - Hướng dẫn sử dụng" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/He-thong-NukeViet-20/" title="Tin Tức - Hệ thống NukeViet 2.0" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/He-thong-NukeViet-10/" title="Tin Tức - Hệ thống NukeViet 1.0" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/NukeViet-co-ban/" title="Tin Tức - NukeViet cơ bản" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Web-Internet/" title="Tin Tức - Web & Internet" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Thu-thuat-Internet/" title="Tin Tức - Thủ thuật Internet" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Dich-vu-tien-ich/" title="Tin Tức - Dịch vụ & tiện ích" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Website-nen-vao/" title="Tin Tức - Website nên vào" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Thong-tin-khuyen-mai/" title="Tin Tức - Thông tin khuyến mại" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/thuong-mai-dien-tu/" title="Tin Tức - Thương mại điện tử" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Hau-truong/" title="Tin Tức - Hậu trường" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Blog-Admin/" title="Tin Tức - Blog Admin" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Cong-tac-vien/" title="Tin Tức - Cộng tác viên" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Chuyen-cong-nghe/" title="Tin Tức - Chuyện công nghệ" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Chuyen-kinh-doanh/" title="Tin Tức - Chuyện kinh doanh" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Cuoc-song-quanh-ta/" title="Tin Tức - Cuộc sống quanh ta" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Cuoi-thu-gian/" title="Tin Tức - Cười thư giãn" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Lang-nghe-va-suy-ngam/" title="Tin Tức - Lắng nghe và suy ngẫm" type="application/rss+xml">
<link rel="alternate" href="https://mangvn.org/rss/Kien-thuc-Song/" title="Tin Tức - Kiến thức Sống" type="application/rss+xml">
<link rel="preload" as="style" href="https://mangvn.org/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://mangvn.org/themes/default/css/bootstrap.min.css" type="text/css">
<link rel="preload" as="style" href="https://mangvn.org/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://mangvn.org/themes/default/css/style.responsive.css" type="text/css">
<link rel="preload" as="style" href="https://mangvn.org/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://mangvn.org/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://mangvn.org/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://mangvn.org/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://mangvn.org/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://mangvn.org/themes/default/css/bootstrap.min.css">
<link rel="stylesheet" href="https://mangvn.org/themes/default/css/style.css">
<link rel="stylesheet" href="https://mangvn.org/themes/default/css/style.responsive.css">
<link rel="StyleSheet" href="https://mangvn.org/themes/default/css/news.css">
<link rel="stylesheet" href="https://mangvn.org/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">MangVN</h2>
		<p class="pull-right"><a title="MangVN" href="https://mangvn.org/">https://mangvn.org</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Giới thiệu về mảng hai chiều trong JavaScript</h1>
		<ul class="list-inline">
			<li>Thứ bảy - 15/10/2011 02:41</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
						<div class="pull-left imghome">
				<img alt="Giới thiệu về mảng hai chiều trong JavaScript" src="https://mangvn.org/assets/news/2011_10/2darray01.jpg" width="100" class="img-thumbnail" />
			</div>
			Trong bài viết dưới đây, chúng tôi sẽ giới thiệu và hướng dẫn các bạn  một số thao tác cơ bản để tạo và truy cập tới mảng 2 chiều – 2D array  trong JavaScript. Về mặt bản chất, mảng 2 chiều là khái niệm về chuỗi  các ma trận – matrix, được sử dụng để chứa thông tin. Mỗi 1 thành phần  lại có chứa 2 chỉ số riêng biệt: row (y) – dòng và column (x) – cột...
		</div>
		<div id="bodytext" class="clearfix">
			<p><span style="color: rgb(51, 51, 51);"><strong>Quản Trị Mạng - Trong bài viết dưới đây, chúng tôi sẽ giới thiệu và hướng dẫn các bạn một số thao tác cơ bản để tạo và truy cập tới mảng 2 chiều – 2D array trong JavaScript. Về mặt bản chất, mảng 2 chiều là khái niệm về chuỗi các ma trận – matrix, được sử dụng để chứa thông tin. Mỗi 1 thành phần lại có chứa 2 chỉ số riêng biệt: row (y) – dòng và column (x) – cột. Ma trận sẽ tiến hành xử lý mỗi khi bạn nhập vào dòng và cột dữ liệu.</strong></span> Ví dụ: nếu bạn muốn hiển thị dữ liệu trên từng dòng với mỗi năm và từng cột với mỗi tháng, hoặc khi bạn thực hiện quá trình tạo báo cáo tài chính cho công ty, tổ chức, doanh nghiệp... thì việc sử dụng matrix là 1 giải pháp vô cùng hợp lý, tương ứng với tình hình kinh doanh và hiệu suất của từng phòng, ban ngành... hoặc đem so sánh với mức chỉ tiêu chung của toàn công ty.</p><p style="text-align: center;"><img align="absMiddle" src="http://www.quantrimang.com.vn/photos/image/102011/15/2darray--02.jpg" alt="" /></p><p>Các bạn hãy liên tưởng tới 1 chiếc bàn cờ, có 8 dòng (được đánh dấu từ <strong>0 &gt; 7</strong>), 8 cột (cũng đánh dấu từ <strong>0 &gt; 7</strong>). Nếu muốn kiểm tra vị trí chính xác của ô phía trên góc trái thì hãy truy cập <strong>chessboard[0][0]</strong>, ô tiếp theo sẽ là vị trí<strong> [0][1]</strong>, và tiếp tục thay thế giá trị của x thành <strong>[0][7]</strong> để đi hết 1 cạnh từ trái sang phải của bàn cờ. Tương tự như vậy, thay đổi giá trị y nếu muốn di chuyển từ trên xuống dưới, ví dụ <strong>[6][1]</strong> là ô thứ <strong>2</strong> của cột thứ <strong>7</strong>.</p><p>Ký hiệu chúng tôi đang sử dụng ở đây khá phù hợp với những biến trong <strong>JavaScript: [y][x]</strong>, tất cả các mảng đều bắt đầu từ 0, do vậy có thể hiểu nôm na về dạng câu hỏi: “Khoảng cách bao nhiêu tính từ phía trái” hoặc “vị trí 0” sẽ chính xác là tọa độ đầu tiên từ bên trái.</p><p>Một trong những quy ước chung được sử dụng rộng rãi khi áp dụng với ma trận là dùng chung biến x và y, ví dụ x luôn luôn là chỉ số cột (khoảng cách, vị trí tính từ bên trái), và y là chỉ sổ dòng (khoảng cách từ trên xuống). Do vậy, tọa độ <strong>y,x</strong> tương ứng với <strong>[0][0]</strong> là thành phần đầu tiên ở góc trên bên trái, <strong>[0][1]</strong> là thành phần thứ 2 tiếp theo từ trên xuống, còn <strong>[1][n]</strong> là dòng thứ 2....</p><h2>JavaScript và 2D Array:</h2><p>Nhưng trên thực tế, <strong>JavaScript </strong>lại không hỗ trợ <strong>2D Array</strong>. Và cách thường sử dụng để xử lý dữ liệu trong mảng 2 chiều là tạo đối tượng Array, bao gồm nhiều đối tượng <strong>Array </strong>bên trong.</p><h3>Sử dụng mảng 1 chiều tương tự như mảng 2 chiều:</h3><p>Trong nhiều trường hợp thực tế, chúng ta có thể dễ dàng “ép” mảng 1 chiều vào trong mảng 2 chiều bàng cách sử dụng toán tử lập trình logic. Ví dụ: chúng ta có 1 chuỗi các ký tự như sau:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var sData= &quot;abcdefghijABCDEFGHIJäß©ÐëØ&gt;½îÿ&quot;;</span></em></p><p style="text-align: left;">Tại đây, chúng ta có thể dễ dàng thấy 1 mô hình đơn giản, với 3 chuỗi liên tiếp của 10 ký tự, tương ứng với đó là ma trận <strong>10 x 3</strong>:</p><p style="text-align: left; margin-left: 40px;"><em><span style="font-family: Courier New;">// column:  0123456789 <br />var sData= &quot;abcdefghij&quot;  // row 0 (starts at offset 0)<br />          +&quot;ABCDEFGHIJ&quot;  // row 1 (starts at offset 10)<br />          +&quot;äß©ÐëØ&gt;½îÿ&quot;  // row 2 (starts at offset 20)</span></em></p><p style="text-align: left;">Mỗi dòng tại đây sẽ chứa 10 cột, chúng ta có thể dễ dàng tính toán và xác định vị trí đầu tiên của bất kỳ dòng nào bằng cách nhân số dòng với 10. Công thức tổng quát tại đây sẽ có dạng:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">(y * row_length) + x</span></em></p><p>với<strong> y</strong> là số dòng,<strong> x </strong>là số cột, <strong>row_length</strong> là số cột trong mỗi dòng.</p><p style="text-align: center;"><img align="absMiddle" src="http://www.quantrimang.com.vn/photos/image/102011/15/2darray--03.jpg" alt="" /></p><p>Với dữ liệu như ví dụ trên, chúng ta có thể dựng cấu trúc mảng 2 chiều như sau:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">// assumes data is a string, sData and rows have 10 columns<br />function GetCellValue(y,x) {<br />nRowStart= y*10;<br />nOffset = nRowStart+ x;<br />sElementValue= sData.substr(nOffset,1); // access one element   <br />return( sElementValue );  <br />}<br />....<br />//                  y,x (row, column)<br />alert( GetCellValue(0,0) ); // displays a<br />alert( GetCellValue(0,1) ); // displays b<br />alert( GetCellValue(0,2) ); // displays c<br />alert( GetCellValue(1,2) ); // displays C<br />alert( GetCellValue(2,2) ); // displays ©<br />alert( GetCellValue(0,9) ); // displays j<br />alert( GetCellValue(1,9) ); // displays J<br />alert( GetCellValue(2,9) ); // displays ÿ</span></em></p><p>Sau đó, chúng ta đã có thể truy cập chuỗi dữ liệu tương tự như mảng 2 chiều với từng ký tự riêng biệt. Và tiếp tục như vậy, chúng ta hoàn toàn làm được việc ép cấu trúc mảng 2 chiều vào mảng 1 chiều bằng cách gán thêm chức năng truy cập dữ liệu đặc biệt. Nhưng đây cũng không phải là cách làm phổ biến trong <strong>JavaScript</strong>.</p><h2>Sử dụng mảng của mảng:</h2><p>Trên thực tế, cách sử dụng thường xuyên mảng 2 chiều trong <strong>JavaScript </strong>là tạo mảng 1 chiều, sau đó gán từng đối tượng bên trong đó với 1 mảng 1 chiều khác. Nếu đi vào việc phân tích cụ thể, chức năng dưới đây là 1 trong những cách đơn giản để tạo và cố định mảng 2 chiều:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">as2D= new Array(); // an array of &quot;whatever&quot;<br />as2D[0]= new Array(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot; );<br />as2D[1]= new Array(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot; );<br />as2D[2]= new Array(&quot;ä&quot;,&quot;ß&quot;,&quot;©&quot;,&quot;Ð&quot;,&quot;ë&quot;,&quot;Ø&quot;,&quot;&gt;&quot;,&quot;½&quot;,&quot;î&quot;,&quot;ÿ&quot; );</span></em></p><p>Khi đó, chúng ta đã có thể xây dựng và xác định được mảng dữ liệu với 3 đối tượng, mỗi đối tượng có 10 chuỗi ký tự khác nhau. Và bây giờ, tiếp tục sử dụng cú pháp <strong>JavaScript </strong>để truy cập như bình thường:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">alert( as2D[0][0] ); // displays a<br />alert( as2D[2][2] ); // displays ©<br />alert( as2D[2][9] ); // displays ÿ</span></em></p><p style="text-align: center;"><img align="absMiddle" src="http://www.quantrimang.com.vn/photos/image/102011/15/2darray--04.jpg" alt="" /></p><h3>Sử dụng […]:</h3><p>Cú pháp:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var a= [ item0, item1, item2, ... ];</span></em></p><p>là cách viết tắt của:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var a= new Array( item0, item1, item2, ... );</span></em></p><p>Qua đó, chúng ta có thể hiểu nôm na rằng:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">[] tương tự với mảng mới và không có dữ liệu<br /></span></em></p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">[&quot;a&quot;] tương tự với mảng mới với 1 chuỗi dữ liệu<br /></span></em></p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">[&quot;a&quot;,&quot;b&quot;] tương tự với mảng mới với 2 chuỗi dữ liệu</span></em></p><p>và tiếp tục như vậy. Do đo, các bạn có thể xác định và xây dựng mảng dữ liệu như trên bằng cú pháp:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D = [ <br />    [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;], <br />    [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;], <br />    [&quot;ä&quot;,&quot;ß&quot;,&quot;©&quot;,&quot;Ð&quot;,&quot;ë&quot;,&quot;Ø&quot;,&quot;&gt;&quot;,&quot;½&quot;,&quot;î&quot;,&quot;ÿ&quot;] <br />];</span></em></p><p>Với cú pháp như vậy, <strong>JavaScript </strong>có thể dễ dàng xây dựng được biến theo dạng mảng, tương tự như cú pháp:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">as2D[n]= new Array( a,b,c,... )</span></em></p><p>đã được sử dụng trước đó. Và cách truy cập dữ liệu cũng không có gì khác.</p><h3>Sử dụng chức năng Array.push():</h3><p>Hàm <strong>push()</strong> khi được áp dụng vào các đối tượng sẽ thực hiện chức năng gán đối tượng (hoặc chuỗi) mới tới vị trí cuối cùng. Cách này thường được dùng để xác định 1 mảng nào đó từ khởi đầu, chúng ta có thể sử dụng cú pháp:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D = new Array();<br />as2D[0] = new Array();<br />as2D[0].push( &quot;a&quot; );<br />as2D[0].push( &quot;b&quot; );<br />as2D[0].push( &quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot; );<br />as2D[0].push( &quot;j&quot; );<br />as2D.push( new Array( &quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot; ) );<br />as2D.push( [ &quot;ä&quot;,&quot;ß&quot;,&quot;©&quot;,&quot;Ð&quot;,&quot;ë&quot;,&quot;Ø&quot;,&quot;&gt;&quot;,&quot;½&quot;,&quot;î&quot;,&quot;ÿ&quot; ] ); </span></em></p><p>Cú pháp trên được dùng để tạo đối tượng có dạng mảng trong mảng, và cách thức hoạt động tương tự như ví dụ trên. Tuy nhiên, các bạn cần lưu ý rằng hàm <strong>push()</strong> cho phép người dùng dồn các phần dữ liệu đơn (như dòng <strong>3,4 và 6</strong>) hoặc dữ liệu kép (dòng <strong>5</strong>), còn dòng <strong>7 và 8</strong> sẽ dồn toàn bộ các dữ liệu vào vị trí top của mảng. Chúng ta có thể thấy sự khác biệt so với ví dụ trên khi không có minh họa:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D= []; // or: new Array();<br />as2D.push( [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;] );<br />as2D.push( [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;] );<br />as2D.push( [&quot;ä&quot;,&quot;ß&quot;,&quot;©&quot;,&quot;Ð&quot;,&quot;ë&quot;,&quot;Ø&quot;,&quot;&gt;&quot;,&quot;½&quot;,&quot;î&quot;,&quot;ÿ&quot;] ); </span></em></p><h3>Sử dụng chức năng String.split():</h3><p>Hàm <strong>split()</strong> của đối tượng <strong>String </strong>trong <strong>JavaScript </strong>sẽ trả về đối tượng <strong>Array</strong>, và rất được sử dụng thường xuyên trong việc cố định Array với các biến đã được khởi tạo trước:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var sData1= &quot;a,b,c,d,e,f,g,h,i,j&quot;;<br />var sData2= &quot;A,B,C,D,E,F,G,H,I,J&quot;;<br />var sData3= &quot;ä,ß,©,Ð,ë,Ø,&gt;,½,î,ÿ&quot;;<br />var as2D= []; // or: new Array();<br />as2D[0]= sData1.split(&quot;,&quot;);<br />as2D[1]= sData2.split(&quot;,&quot;);<br />as2D[2]= sData3.split(&quot;,&quot;);</span></em></p><p>Tham số thứ 2 trong hàm <strong>split()</strong> có chức năng xác nhận tất cả các ký tự phân cách, trong trường hợp này chúng tôi sử dụng dấu phẩy. Có 1 quy luật như sau: nếu ký tự phân cách có dạng rỗng <strong>(“”)</strong>, thì kết quả trả về sẽ là mảng dữ liệu cá ký tự riêng biệt.</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var sData1= &quot;abcdefghij&quot;;<br />var sData2= &quot;ABCDEFGHIJ&quot;;<br />var sData3= &quot;äß©ÐëØ&gt;½îÿ&quot;;<br />var as2D= []; // or: new Array();<br />as2D[0]= sData1.split(&quot;&quot;);<br />as2D[1]= sData2.split(&quot;&quot;);<br />as2D[2]= sData3.split(&quot;&quot;);</span></em></p><p>hoặc:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D= []; <br />as2D[0]= &quot;abcdefghij&quot;.split(&quot;&quot;);<br />as2D[1]= &quot;ABCDEFGHIJ&quot;.split(&quot;&quot;);<br />as2D[2]= &quot;äß©ÐëØ&gt;½îÿ&quot;.split(&quot;&quot;);</span></em></p><p>hoặc:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D= &quot;abcdefghij,ABCDEFGHIJ,äß©ÐëØ&gt;½îÿ&quot;.split(&quot;,&quot;)<br />as2D[0]=as2D[0].split(&quot;&quot;);<br />as2D[1]=as2D[1].split(&quot;&quot;);<br />as2D[2]=as2D[2].split(&quot;&quot;);</span></em></p><p>hoặc thậm chí là:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var as2D= [<br />    &quot;abcdefghij&quot;.split(&quot;&quot;),<br />    &quot;ABCDEFGHIJ&quot;.split(&quot;&quot;),<br />    &quot;äß©ÐëØ&gt;½îÿ&quot;.split(&quot;&quot;)<br />];</span></em></p><p>Nếu đem so sánh đoạn mã cuối cùng với C++ thì JavaScript có một chút khác biệt: việc khai báo var chỉ là 1 phần của thủ tục được thực hiện trong quá trình thực thi.</p><h2>Tạo mảng 2 chiều:</h2><h3>Sử dụng lệnh lặp for:</h3><p>Lý do chính để tạo và sử dụng mảng 2 chiều là tại một thời điểm hoặc vị trí nào đó trong toàn bộ chương trình, chúng ta bắt buộc phải dùng các cấu trúc lệnh lặp nhau. Ví dụ:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">for ( var y=0; y&lt;3; y++ ) {<br />    for ( var x=0; x&lt;10; x++ ) { <br />       // do something with myArray[y][x]<br />    }<br />}</span></em></p><p>Và quá trình này để phục vụ chương trình khi đi qua từng dòng và cột để truy cập dữ liệu tại những vị trí tương ứng. Ví dụ:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var sOut=&quot;<table border="2">&quot;;<br />for (var y=0; y&lt;as2D.length; y++ ) {        // for each row<br />    sOut += &quot;<tr>&quot;;<br />    for (var x=0; x&lt;as2D[y].length; x++ ) { // for each clm<br />        sOut += &quot;<td>&quot; + as2D[y][x] + &quot;</td>&quot;;<br />    }<br />    sOut += &quot;</tr>&quot;;<br />}<br />sOut += &quot;</table>&quot;;</span></em></p><p>sẽ tạo ra trang <strong>HTML</strong> có dạng như hình dưới:</p><p style="text-align: center;"><img align="absMiddle" src="http://www.quantrimang.com.vn/photos/image/102011/15/2darray--05.jpg" alt="" /></p><p>Và nếu thay đổi vị trí của dòng và cột cho nhau:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var nClmsPerRow= as2D[0].length;      // assume same length<br />for ( var x=0; x&lt;nClmsPerRow; x++ ) {  // for each row<br />    sOut += &quot;<tr>&quot;;<br />    for ( var y=0; y&lt;as2D.length; y++ ) { // for each clm<br />       sOut += &quot;<td>&quot; + as2D[y][x] + &quot;</td>&quot;;<br />    }<br />    sOut += &quot;</tr>&quot;;<br />}</span></em></p><p>Thì bảng của chúng ta sẽ có 10 dòng và 3 cột:</p><p style="text-align: center;"><img align="absMiddle" src="http://www.quantrimang.com.vn/photos/image/102011/15/2darray--06.jpg" alt="" /></p><h3>Sử dụng lệnh lặp for...in:</h3><p>Bên cạnh đó, <strong>JavaScript </strong>còn cung cấp cho người sử dụng cấu trúc lặp lệnh khá đặc biệt thông qua mảng dữ liệu, đó là hàm <strong>for... in</strong>. Việc sử dụng chức năng này khá đơn giản khi đã biết rõ về điều kiện kết thúc vòng lặp (phần cuối cùng trong mảng dữ liệu). Và nó được sử dụng cùng với <strong>Collection và Array</strong>. Với Array thì cú pháp chung sẽ có dạng:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">for ( nIdxVar in aArray )</span></em></p><p>Mỗi 1 vòng lặp sẽ thiết lập <strong>nIdxVar</strong> thành chỉ số lặp đi lặp lại (0, 1, 2,...), và quy trình này sẽ kết thúc khi tới vị trí cuối cùng trong mảng. Dưới đây là 1 vài đoạn mã có chức năng truy cập tới tất cả các thành phần trong ví dụ mảng 2 chiều bên trên:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">for ( y in as2D ) {<br />   for ( x in as2D[y] ) {<br />       // do something with as2D[y][x];<br />   }<br />}</span></em></p><p>Phần giá trị thực của <strong>for...in</strong> sẽ xuất hiện khi chúng ta có sparse array ; cụ thể là trường hợp một số thành phần chưa được xác định rõ. Ví dụ như sau:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">var aSparse= new Array;<br />aSparse[0]= [&quot;zero&quot;,  &quot;one&quot;,       &quot;two&quot;      ];<br />aSparse[4]= [      ,  &quot;forty-one&quot;,            ];<br />aSparse[5]= [&quot;fifty&quot;, &quot;fifty-one&quot;, &quot;fifty-two&quot;];<br />for ( y in aSparse ) {<br />   for ( x in aSparse[y] ) {<br />       alert(&quot;y,x=(&quot; +y+ &quot;,&quot; +x+ &quot;) value: &quot; + aSparse[y][x] );<br />   }<br />}</span></em></p><p>sẽ bỏ qua các dòng từ 1 &gt; 3, cột 0 và 2 của dòng 4, toàn bộ giá trị trong đây sẽ không được xác định. Và kết quả trả về tại đây sẽ có dạng:</p><p style="margin-left: 40px;"><em><span style="font-family: Courier New;">y,x=(0,0) = 0<br />y,x=(0,1) = 1<br />y,x=(0,2) = 2<br />y,x=(4,1) = 40 – 1 <br />y,x=(5,0) = 50<br />y,x=(5,1) = 50 – 1 <br />y,x=(5,2) = 50 – 2</span></em></p><h3>So sánh mảng 2 chiều và bộ tổ hợp đối tượng:</h3><p>Trên thực tế, mảng 2 chiều rất phù hợp trong việc “đại diện” cho 1 ma trận với các đối tượng đã được xác định sẵn. Ví dụ: 1 bàn cờ có 8 x 8 ô, 1 màn hình sẽ có <strong>1680 x 1050</strong> ma trận điểm ảnh... Và do vậy, công thức <strong>x – y</strong> ma trận thường xuyên được sử dụng trong những chương trình nâng cao được viết bằng <strong>JavaScript</strong>.</p><p>Những trường hợp phổ biến hơn là mảng 1 chiều chính là danh sách các nhóm của nhiều đối tượng, thành phần khác nhau. Và cấu trúc này thường được áp dụng trong bộ tổ hợp cơ sở dữ liệu, danh sách các đối tượng <strong><input /></strong> trên 1 trang web, các dữ liệu có liên quan tới quy trình shopping cart... Chúc các bạn thành công!</p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả:</strong>
				T.Anh (theo Experts Exchange)
			</p>
						<p>
				<strong>Nguồn tin:</strong>
				http://www.quantrimang.com.vn
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://mangvn.org/savefile/Thu-thuat-tin-hoc/Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html" title="Giới thiệu về mảng hai chiều trong JavaScript">https://mangvn.org/savefile/Thu-thuat-tin-hoc/Gioi-thieu-ve-mang-hai-chieu-trong-JavaScript-2931.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; MangVN
		</div>
		<div id="contact">
			<a href="mailto:admin@vinades.vn">admin@vinades.vn</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://mangvn.org/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<script src="https://mangvn.org/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_b41EI",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=1,nv_recaptcha_ver=2,nv_recaptcha_sitekey="6LfaEikpAAAAAB7BnT8L5ijh2CCG0esJquuWj9ZJ",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://mangvn.org/assets/js/language/vi.js"></script>
<script src="https://mangvn.org/assets/js/DOMPurify/purify3.js"></script>
<script src="https://mangvn.org/assets/js/global.js"></script>
<script src="https://mangvn.org/assets/js/site.js"></script>
<script src="https://mangvn.org/themes/default/js/news.js"></script>
<script src="https://mangvn.org/themes/default/js/main.js"></script>
<script src="https://mangvn.org/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://mangvn.org",
            "logo": "https://mangvn.org/uploads/logo-mangvn.png"
        }
        </script>
<script src="https://mangvn.org/themes/default/js/bootstrap.min.js"></script>
</body>
</html>