0

This snippet of code has the values I list in the comments.

var studentid = course.roster[i];     // studentid = 1367
console.log(studentid);               // prints 1367
console.log(students[1367]);          // correctly prints a student object

If I change the key in the third line to use a variable with the exact same value, then the result is undefined. Why?

console.log(students[studentid]);     // fails to print correctly

This is the output from console.log(students[1367]):

Object {
    lname: "John",
    fname: "Smith",
    grade: "Sophomore",
    id: "1367",
    email: "[email protected]"
}

This is the output from console.log(students[studentid]):

undefined
2
  • 2
    may be studentid is return string Commented Apr 26, 2016 at 5:05
  • does this solve your problem? Commented Apr 26, 2016 at 5:07

2 Answers 2

2

The problem is

var studentid = course.roster[i];     // studentid = 1367

its return string and your id in integer so try to convert the studentid into string

var studentid = parseInt(course.roster[i]);     // studentid = 1367

which will work in your case

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

Comments

0

try to parse the string id to number.

console.log(students[Number(studentid)]);

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.