1

Im trying to pull the gpa numbers from the array that is in the object and have them displayed in the console but my code keeps giving me undefined '0' error. Any help would be appreciated.

var fsInfo = {
    name: 'John Doe',
    address:{
        street: '123 Some Street ',
        city: 'Town, ',
        state: 'HI',
    gpa: [3.0,4.0,2.0]
    }
 }

console.log("GPA: " + fsInfo.gpa['0'],fsInfo.gpa['1'],fsInfo.gpa['2'])

1 Answer 1

1

Use

console.log("GPA: " + fsInfo.gpa[0],fsInfo.gpa[1],fsInfo.gpa[2])

Note: Array indices are numbers.

In your case, they are inside address. So you should do

console.log("GPA: " + fsInfo.address.gpa[0],fsInfo.address.gpa[1],fsInfo.address.gpa[2])

If your object had been like this

var fsInfo = {
    name: 'John Doe',
    address:{
        street: '123 Some Street ',
        city: 'Town, ',
        state: 'HI'
    },
    gpa: [3.0,4.0,2.0]
 }

then

console.log("GPA: " + fsInfo.gpa[0],fsInfo.gpa[1],fsInfo.gpa[2])

will work.

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

2 Comments

You can use non-integer indices, but in JavaScript that's an object. In this object, gpa is a non-numeric index to the array. You're correct here, though - the indexes are int, not string.
Wow didn't even see I closed it in the wrong location. I feel so stupid lol. Thanks alot

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.