1

Why am I getting the error message: "Line[i] is undefined" in the following code? It might be something trivial, but I just can't fix it.

It works, for example, if I use Line[0] or Line[1], etc.

But I need to iterate through the loop.

var Line = [{ Func: "Function"}];
for (i = 0; i <= 15; i++) {
    Line[i].Func = "Function" + I;
}
2
  • 1
    your Line is one element array [0 => {Func:...}] so Line[1] is undefined, therefore Line[1].Func is undefined Commented Apr 19, 2017 at 15:17
  • 1
    Well, you loop through 16 items of a single-item array... Are you trying to append new items? Commented Apr 19, 2017 at 15:18

5 Answers 5

3

You are trying to iterate to i <= 15 and getting error when JavaScript evaluate Line[1] because it is undefined

Instead you can iterate til the length of Line array:

var Line = [{ Func: "Function"}];
for (var i = 0, len = Line.length; i < len; i++) {
  Line[i].Func = "Function" + i;
}

console.log(Line);

Or iterate using Array.prototype.forEach():

var Line = [{ Func: "Function"}];
Line.forEach((el, index) => el.Func = "Function" + index);

console.log(Line);

Or do your way but first check the element Line[i] exist:

var Line = [{ Func: "Function"}];
for (var i = 0; i <= 15; i++) {
  Line[i] && (Line[i].Func = "Function" + i);
  console.log(i);
}

console.log(Line);

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

Comments

1

Because you have only one element in the array.

var Line = [{ Func: "Function"}];
for (i = 0; i <= 15; i++) { console.log(i); Line[i].Func = "Function" + i;}

will print 0 and 1 and then it will throw an error

Comments

0

You are looping from 0 through to 15, and using the loop variable as an index into an array that only has 1 element (index=0)

Looks like you're trying to add new elements

var Line = [{ Func: "Function"}];
for (i = 0; i <= 15; i++) {
    Line[i] = {Func:"Function" + i}; 
}

Comments

0

You have a one-length array and you're iterating up to 15. So, you can either check the array limit, or provide a default value up to 15.

This is how you can provide a default value up to 15:

var Line = [{ Func: "Function"}];
for (var i = 0; i <= 15; i++) {
  if (Line[i] === undefined) {
      Line[i] = { Func: "Function"}
  }
  Line[i].Func = "Function" + i;
}

On the other hand, this is how you can go through the actual list, regardless of the element number:

var Line = [{ Func: "Function"}];
for (var i = 0; i <= Line.length; i++) {
  Line[i].Func = "Function" + i;
}

Comments

0

Your Line array has only 1 object inside it and you are looping from 0 to 15, so after Line[0] it cannot find Line[1] or Line[2]. You probably want to add an object 15 times to Line. Try something like this within your for loop:

Line.push({Func:"Function" + i});

https://www.w3schools.com/jsref/jsref_push.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.