Single dimensional array vs multidimensional array in JavaScript.

JavascriptWeb DevelopmentObject Oriented Programming

Following is the code for single and multidimentsional array in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .result,.sample {
      font-size: 18px;
      font-weight: 500;
      color: rebeccapurple;
   }
   .result {
      color: red;
   }
</style>
</head>
<body>
<h1>Single dimensional array vs multidimensional array</h1>
<div class="sample">Single dimensional Array<br /></div>
<div class="result">Multidimensional Array<br /></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to display single and multidimensional array</h3>
<script>
   let resEle = document.querySelector(".result");
   let sampleEle = document.querySelector(".sample");
   let BtnEle = document.querySelector(".Btn");
   let arr = [1, 2, 3, 4, 5];
   let arr1 = [
      [1, 2, 3],
      ["A", "B", "C"],
      [5, "D", 6],
   ];
   BtnEle.addEventListener("click", () => {
      sampleEle.innerHTML += arr + "";
      arr1.forEach((item, index) => {
         resEle.innerHTML += "Index : " + index + " array = " + item + "";
      });
   });
</script>
</body>
</html>

Output

The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −

raja
Published on 21-Jul-2020 07:12:16
Advertisements