0

This is linked to my previous question however its slightly different and is a whole new question... I have an array, which after some nice PHP looks like this:

var series = {
    "001":{
        "game":"Portal 2",
        "name":"Portal 2"
    },
    "002":{
        "game":"Minecraft",
        "name":"241"
    },
    "003":{
        "game":"Minecraft",
        "name":"HackMine"
    },
    "004":{
        "game":"Mass Effect 3",
        "name":"Mass Effect 3"
    },
    "005":{
        "game":"League of Legends",
        "name":"League of Legends"
    },
    "006":{
        "game":"Half Life 2",
        "name":"The Hidden: Source"
    },
    "007":{
        "game":"Skyrim",
        "name":"Modded Skyrim"
    }
}

This bit is populated fine... Now my question is when this function is called, it always gives game and name the value undefined

function seriesIdOnBlur()
{       

    var id = parseInt(document.getElementById("series_id").value);

    if (series[id] == null)
    {
        var message = "The Series ID you input was invalid";
    }
    else
    {
        var seriesId = series[id];

        var game = seriesId['game'];
        var name = seriesId['name'];
        var message = "You've inputted the id for the game: " + game + " for the series: " + name;
    }

    document.getElementById("series_id_check").innerHTML = message;
}
1
  • 1
    Those are not arrays. That is an object. Commented Aug 7, 2013 at 23:05

2 Answers 2

6

You're shooting yourself in the foot with those leading 0s: series['001'] is not the same as series['1']. Either cut the parseInt call (forcing the user to type '001'), drop the leading 0s:

var series = {
    "1":{
        "game":"Portal 2",
        "name":"Portal 2"
    },
    ...
    "7":{
        "game":"Skyrim",
        "name":"Modded Skyrim"
    }
}

or use an array:

var series = [
    {
        "game":"Portal 2",
        "name":"Portal 2"
    },
    ...
    {
        "game":"Skyrim",
        "name":"Modded Skyrim"
    }
]
Sign up to request clarification or add additional context in comments.

6 Comments

Note that arrays are 0 based, so if you switch to the array example your first element will be at index 0 instead of "001". If you want it to be at index 1 you can just specify your indexes and create what is called a "sparse" array: series[1] = {"game":"Portal", "name":"Portal"};
quick plunker for the variant without the parseInt: plnkr.co/edit/tXlIUddSKkWeIOvcQUY2?p=preview
I'm using the 00# intentionally, as eventually I will have more than 10 series, and I want the database to look neat, I don't expect to go above 999 so it's fine with the zeros... so you say to drop the parseInt
Plunker is really useful, however, on plunker it works, and on the website it doesn't any idea? Website is upload.red-jax.com
thanks I found my issue this solution did work, it was where I was getting the PHP i surrounded it in quotes... So it was nothing to do with this error, it was me cleaning up when I posted to SO... Thanks :D
|
1

This:

series = {
    "001":{
        "game":"Portal 2",
        "name":"Portal 2"
    },
    "002":{
        "game":"Minecraft",
        "name":"241"
    } ... };

is an object literal so you have Object of Objects rather than Array of Arrays as you stated.

You will need array of objects in order your code to work:

series = [
    {
        "game":"Portal 2",
        "name":"Portal 2"
    },
    {
        "game":"Minecraft",
        "name":"241"
    } ... ];

1 Comment

Object of objects is fine, I thought they were arrays

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.