{"id":1121,"date":"2024-07-28T09:35:03","date_gmt":"2024-07-28T09:35:03","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1121"},"modified":"2025-05-15T04:52:51","modified_gmt":"2025-05-15T04:52:51","slug":"how-to-get-arrays-inside-an-array-javascript","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/how-to-get-arrays-inside-an-array-javascript\/","title":{"rendered":"How to get arrays inside an array Javascript"},"content":{"rendered":"\n<p>In JavaScript, arrays can contain other arrays, creating a multi-dimensional array or an array of arrays. To access elements inside an array of arrays, you use multiple bracket notation. Here\u2019s a guide on how to get and manipulate arrays inside an array:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Creating an Array of Arrays<\/strong><\/h3>\n\n\n\n<p>You can create an array of arrays by nesting arrays within a main array:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;react&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let arrayOfArrays = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Accessing Elements in an Array of Arrays<\/strong><\/h3>\n\n\n\n<p>To access elements inside an array of arrays, use bracket notation twice: the first bracket to access the nested array, and the second to access the element within that nested array.<\/p>\n\n\n\n<p><strong>Access a Nested Array:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let firstArray = arrayOfArrays[0]; \/\/ [1, 2, 3]\nlet secondArray = arrayOfArrays[1]; \/\/ [4, 5, 6]<\/pre><\/div>\n\n\n\n<p>Access an Element in a Nested Array:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let firstElement = arrayOfArrays[0][0]; \/\/ 1\nlet secondElement = arrayOfArrays[1][2]; \/\/ 6<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Iterating Over an Array of Arrays<\/strong><\/h3>\n\n\n\n<p>You can use nested loops to iterate over an array of arrays.<\/p>\n\n\n\n<p><strong>Using <code>for<\/code> Loop:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">for (let i = 0; i &lt; arrayOfArrays.length; i++) {\n  for (let j = 0; j &lt; arrayOfArrays[i].length; j++) {\n    console.log(arrayOfArrays[i][j]);\n  }\n}<\/pre><\/div>\n\n\n\n<p>Using <code>forEach<\/code> Loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">arrayOfArrays.forEach(innerArray =&gt; {\n  innerArray.forEach(element =&gt; {\n    console.log(element);\n  });\n});\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Adding Elements to an Array of Arrays<\/strong><\/h3>\n\n\n\n<p>You can add elements to both the main array and the nested arrays.<\/p>\n\n\n\n<p><strong>Add a New Nested Array:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">arrayOfArrays.push([10, 11, 12]);\nconsole.log(arrayOfArrays);\n\/\/ Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]<\/pre><\/div>\n\n\n\n<p>Add an Element to an Existing Nested Array:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">arrayOfArrays[0].push(0);\nconsole.log(arrayOfArrays[0]);\n\/\/ Output: [1, 2, 3, 0]<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Removing Elements from an Array of Arrays<\/strong><\/h3>\n\n\n\n<p>You can remove elements using methods like <code>pop<\/code>, <code>shift<\/code>, or <code>splice<\/code>.<\/p>\n\n\n\n<p><strong>Remove a Nested Array:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let removedArray = arrayOfArrays.pop();\nconsole.log(removedArray); \/\/ [10, 11, 12]\nconsole.log(arrayOfArrays);\n\/\/ Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/pre><\/div>\n\n\n\n<p>Remove an Element from a Nested Array:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let removedElement = arrayOfArrays[0].pop();\nconsole.log(removedElement); \/\/ 0\nconsole.log(arrayOfArrays[0]);\n\/\/ Output: [1, 2, 3]<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example Usage<\/strong><\/h3>\n\n\n\n<p>Here is an example that demonstrates various operations on an array of arrays:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">let arrayOfArrays = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9]\n];\n\n\/\/ Access elements\nconsole.log(arrayOfArrays[1][1]); \/\/ 5\n\n\/\/ Iterate over array of arrays\narrayOfArrays.forEach(innerArray =&gt; {\n  innerArray.forEach(element =&gt; {\n    console.log(element);\n  });\n});\n\n\/\/ Add elements\narrayOfArrays.push([10, 11, 12]);\narrayOfArrays[0].push(0);\n\n\/\/ Remove elements\narrayOfArrays.pop();\narrayOfArrays[0].pop();\n\nconsole.log(arrayOfArrays);\n<\/pre><\/div>\n\n\n\n<p>Accessing and manipulating arrays inside an array in JavaScript is straightforward using bracket notation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Also Read<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.appfinz.com\/blogs\/how-to-check-if-a-key-exists-in-a-javascript-object\/\">How to Check If a Key Exists in a JavaScript Object<\/a><\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><a href=\"https:\/\/www.appfinz.com\/blogs\/understanding-this-in-javascript\/\">Understanding \u201cthis\u201d in JavaScript<\/a><\/p>\n<\/blockquote>\n\n\n\n<p>If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our\u00a0<a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\"><strong>website designing services<\/strong><\/a>\u00a0and custom\u00a0<strong><a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">website development<\/a><\/strong>\u00a0solutions tailored to your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get Array Inside an Array in Javascript, How to fetch an array inside Javascript complete explained<\/p>\n","protected":false},"author":1,"featured_media":1092,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,150],"tags":[195,196],"class_list":["post-1121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-jquery","tag-array-inside-javascript","tag-javascript-array"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":8,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1351,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1092"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}