I'm using HTML and javascript to make simple web games to play. so giving preset value as an "map element" by using arrays and library.
calling values in an array goes like this:
array = [a,b,c,d]
blah.innerHTML = array[1] // returns "b"
or
lib = {a:"cat",b:"dog",c:[a,b,c,d]}
blah.innerHTML = lib.c.1(or lib.c[1]) // returns "b"
shouldn't it?
well it returns as an error by saying something like "the null value is null" (i had to translate it)
it used to work for sure, but it doesn't anymore... might know why?
used Software: ie8(im in some kind of "closed society"), dreamweaver
3 Answers 3
If a,b,c,d are strings, you should quote them:
lib = {a:"cat",b:"dog",c:['a','b','c','d']}
You need to surround the values in single or double quotes because they are string values.
Comments
You array should be:
array = ['a','b','c','d']
lib = {a:"cat",b:"dog",c:['a','b','c','d']}
https://developer.mozilla.org/enUS/docs/Web/JavaScript/Guide/Grammar_and_Types#Array_literals
Comments
In first snippet array is javascript Array
array = [a,b,c,d]
blah.innerHTML = array[1] // returns "b"
SO array[1] will return the element at index 1.
In this snippet lib is an Object & c is a key of lib
lib = {a:"cat",
b:"dog",
c:['a','b','c','d']}
var _c = lib.c // will give ['a','b','c','d']
var blah = document.getElementById("blow") // id of a DOM element
blah.innerHTML = _c[1] // returns "b"
NOTE:a,b,c,d in [a,b,c,d] will try to refer some variable. Unless declared it will throw error
lib.c[1], also is it['a','b','c','d']or[a,b,c,d]?