Javascript associavtive array -


i want create array javascript

var person = {firstname:"john", lastname:"doe", age:46}; var person = {firstname:"peter", lastname:"toh", age:20}; 

for example want create

person[1].firstname return me john person[2].firstname return me peter 

how declare javascript make 2 element works.

you aren't creating associative array, creating array of objects. can either initialize array literal:

var person = [      {firstname:"john", lastname:"doe", age:46},      {firstname:"peter", lastname:"toh", age:20} ]; 

(keep in mind arrays in javascript start zero, person[0] john , person[1] peter).

or can create empty array , add entries afterwards. allows choose indexes freely:

var person = []; person[1] = {firstname:"john", lastname:"doe", age:46}; person[2] = {firstname:"peter", lastname:"toh", age:20}; 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -