1

I am trying to print out some information from a multi dimensional. In this case it is a First Name, Surname, and birth year.

But when the first person has been printed out, the code breaks and i dont know why. Some help would be greatful.

profiler = [
  ["Jake", "name2", "name3"],
  ["Madsen", "last2", "last3"],
  ["1996-03-09", "year2", "year3"],
];

printProfil(profiler);

function printProfil(profiler) {
  var forNavn = "";
  efterNavn = "";
  fodselsAr = "";
  i = 0;

  for (i; i <= profiler.length; i++) {
    forNavn = profiler[i][i];
    efterNavn = profiler[i + 1][i];
    fodselsAr = profiler[i + 2][i];
    console.log("Fornavn:", forNavn, "Efternavn:", efterNavn, "Fødsels År:", fodselsAr)
  }
}

1

3 Answers 3

2

The reason why the loop is crashing after the first iterate is because of this line:

fodselsAr = profiler[i + 2][i];

In the second loop, i equals 1 -> i + 2 = 3, but profiler only contains 3 entries so it crashes.

You need to access the profiler array with the i index and then access the entries at the 0, 1 and 2 indexes:

profiler = [
  ["Jake", "name2", "name3"],
  ["Madsen", "last2", "last3"],
  ["1996-03-09", "year2", "year3"],
];

printProfil(profiler);

function printProfil(profiler) {
  var forNavn = "";
  efterNavn = "";
  fodselsAr = "";
  i = 0;

  for (i; i < profiler.length; i++) {
    forNavn = profiler[0][i];
    efterNavn = profiler[1][i];
    fodselsAr = profiler[2][i];
    console.log("Fornavn:", forNavn, "Efternavn:", efterNavn, "Fødsels År:", fodselsAr)
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

While this does output the full array, pay close attention to the order that the OP wanted the data displayed in. In their example above, the OP appeared to want the first element from each array on a row, then the second element of each array on a row, etc. This example outputs the contents of each inner array is output as a row which is not the desired output...
Thanks for your answer as well Erazihel, even tho you first answer didnt do what i needed, i did learn something more about arrays :)
1

you can iterate on first element of profiler that ways you will get all names, corresponding to that you can use 0,1,2 for corresponding arrays to get last name and date. If there are going to be more arrays you can use a second loop.

profiler = 
[
["Jake","name2","name3"],
["Madsen","last2","last3"],
["1996-03-09","year2","year3"],
];

printProfil(profiler);

function printProfil(profiler)
{
var forNavn = "";
    efterNavn = "";
    fodselsAr = "";
    i = 0;

for(var i; i < profiler[0].length; i++)
{
        forNavn = profiler[0][i];
        efterNavn = profiler[1][i];
        fodselsAr = profiler[2][i];
        console.log("Fornavn:", forNavn, "Efternavn:", efterNavn, "Fødsels År:", fodselsAr)
    }
}

2 Comments

@JakeTheDane you are welcome, consider accepting the answer if it helped. Cheers!
I just did, i didnt know that was a thing, i am new here, again thanks! :)
0

First, your end condition for your for loop is incorrect. You want to stop iterating once the index equals the array length, so you want to continue looping while the index is less than the length.

EDIT: Let's, myself included, try to understand the data structure you're using so we can determine how to access the data you need. You have an outer array containing an inner array of first names, an inner array of last names, and an inner array of years. The first element of each of these arrays corresponds to a single object. So we want to iterate all three inner arrays in parallel, grabbing an element from each at each iteration.

Since each of the three inner arrays contain one field from a single set of objects, each of the arrays should be the same length. Therefore, when we want to iterate over any one of the arrays, we can use any of their lengths to do it. Let's use the first array for simplicity. A nice way to grab the inner arrays out of your outer array would be with a destructuring assignment. In the for loop, we're iterating over all three inner arrays at once, not the outer array.

With constant indexes:

const firstNames = profiler[0];
const lastNames = profiler[1];
const years = profiler[2];

for(let i = 0; i < firstNames.length; i++) {
  const forNavn = firstNames[i];
  const efterNavn = lastNames[i];
  const fodselsAr = years[i];
  console.log("Fornavn:", forNavn, "Efternavn:", efterNavn, "Fødsels År:", fodselsAr);
}

With array destructuring:

const [firstNames, lastNames, years] = profiler;

for(let i = 0; i < firstNames.length; i++) {
  const forNavn = firstNames[i];
  const efterNavn = lastNames[i];
  const fodselsAr = years[i];
  console.log("Fornavn:", forNavn, "Efternavn:", efterNavn, "Fødsels År:", fodselsAr);
}

1 Comment

You should review the OP's desired output above. This answer does not output the requested format...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.