Ads by ProfitSence
Close

Multidimensional array in JavaScript

Last Updated on Wednesday 5th Oct 2022
  • Array is a type of data structure that groups a lot of data.
  • The purpose of arrays is to make data sets structured and easy to access.
			
					// number data of unknown
const datas = [26, 26, 28, 30, 31, 29, 28, 26];

			
	
  • In JavaScript, an array is possible to hold different types of data.
			
					// jersey number, name, scoring status
const player = [9, 'Harry', true];

			
	

What is a Multidimensional Array?

A multidimensional Array() is nothing more than an Array() with other Array() chained to it, for example, a list, with sub-lists.

  • An array containing arrays as above is also known as a multidimensional array.
  • How to access one of the elements in a multidimensional array?
			
					const  players  =  [
 [9, 'Harry', true],
 [9, 'Jack', true],
 [20, 'Foden', false]
] ;

			
	

How to Access Elements of a Multidimensional Array

			
					console.log(players[0][1]) // Harry

console.log(players[1][2]) // true

			
	

Datas

			
					const  players  =  [
 [9, 'Harry', true],
 [9, 'Jack', true],
 [20, 'Foden', true],
 [20, 'Dominic', false],
 [20, 'Markus', true],
 [20, 'Jadon', false],
 [20, 'Raheem', false]
] ;

			
	

Function for goal not scoring

			
					function goalStatus(datas) {
  // Your code here
  let result = [];
  for (let i = 0; i < datas.length; i++) {
    if (!datas[i][2]) {
      result.push(datas[i]);
    }
  }
  return result;
}

			
	
			
					console.log(goalStatus(players))

// Output
[
 [20, 'Dominic', false],
 [20, 'Jadon', false],
 [20, 'Raheem', false]
] ;