Home > other >  Probleme with empty items in 2d array
Probleme with empty items in 2d array

Time:03-29

I'm trying to build 2d array with JS.

In a theater there is 26 lines, each one include 100 seats :

function theaterSeats() {
  let seats= new Array(26);
  for (let i = 1; i <= 26; i  ){
    seats[i] = new Array(100);
    for (let j = 1; j <= 100; j  ) {
      seats[i][j] = `${i}-${j}`
    }
  }
  return seats;
}

console.log(theaterSeats());

The result is not far from what I expected, except that there is an empty item in each array... I don't understand why. Some help please ?

[
  <1 empty item>,
  [
    <1 empty item>, '1-1',  '1-2',  '1-3',
    '1-4',          '1-5',  '1-6',  '1-7',
    '1-8',          '1-9',  '1-10', '1-11'  

(...................)

Thanks in advance for your answer ;).

CodePudding user response:

Simple map solution

const theaterSeats = [...Array(26)].map((_, i) => {
  return [...Array(100)].map((_, j) => `${i 1}-${j 1}`)
})

console.log(theaterSeats)

CodePudding user response:

JavaScript arrays' index start from 0 that's why your first item is always empty because you have skipped index 0 and started your iteration with index 1. You need to fill your array starting from index 0!

The correct version of your code could be:

function theaterSeats() {
  let seats= new Array(26);
  for (let i = 0; i < 26; i  ){
    seats[i] = new Array(100);
    for (let j = 0; j < 100; j  ) {
      seats[i][j] = `${i   1}-${j   1}`
    }
  }
  return seats;
}

CodePudding user response:

While there are far more elegant ways to setup the 2D-array, I would rather fix your code:

function theaterSeats() {
  let seats = [];
  for (let i = 0; i < 26; i  ) {
    seats[i] = [];
    for (let j = 0; j < 100; j  ) {
      seats[i][j] = `${i 1}-${j 1}`;
    }
  }
  return seats;
}

console.log(theaterSeats());

Note that:

  • Array indexes start from 0, so a 26 item array will have indexes from 0...25
  • You don't really have to size the array when initializing... let a = []; a[9] = "10th item" works as expected

CodePudding user response:

It's just because you're using 1 as the 1st index instead of 0, array index starts in 0, something like:

An array of 7 elements have those indexes: 0,1,2,3,4,5,6. So when setting a value to a position you'll begin like: array[0] = 'some value', array[1] = 'some other value' ...

Here in your for loop you'll need to begin with i and j = 0. So it'll look like

function theaterSeats() {
  let seats= new Array(26);
  for (let i = 0; i <= 26; i  ){
    seats[i] = new Array(100);
    for (let j = 0; j <= 100; j  ) {
      seats[i][j] = `${i 1}-${j 1}`
    }
  }
  return seats;
}

console.log(theaterSeats());

CodePudding user response:

Thanks for your reply ;).

I found the solution before checking your answer. I did that and it's working :

function theaterSieges() {
  let siege = new Array(26);
  for (let i = 0; i < 26; i  ){
    siege[i] = new Array(100);
    for (let j = 0; j < 100; j  ) {
      siege[i][j] = `${i 1}-${j 1}`
    }
  }
  return siege;
}

console.log(theaterSieges());

I will keep in mind your tips.

Thanks again ;)

  • Related