Bdfy Bdfy - 3 months ago 7
Javascript Question

How to use array as key in Javascript?

I have a simple example on python:

programs = {}
if not programs.has_key(( program, time )):
programs[( program, time )] = 0
programs[( program, time )] = programs[( program, time )] + 1


How to use array as key in Javascript ?

Answer

Will this do the trick for you?

jsfiddle

<script>
var ary = {person1:'valerie', person2:'alex'};
for (key in ary) {
    document.write(key, '<br>')
}

document.write(ary['person2'], '<br>')
</script>