Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript set value to Associative Arrays

Tags:

javascript

how set value in javascript to Associative Arrays?

Why in this case i get error: "car[0] is undefined"

var car = new Array();
car[0]['name'] = 'My name';
like image 996
user319854 Avatar asked Dec 09 '22 11:12

user319854


1 Answers

Because you never defined car[0]. You have to initialize it with an (empty) object:

var car = [];
car[0] = {};
car[0]['name'] = 'My name';

Another solution would be this:

var car = [{name: 'My Name'}];

or this one:

var car = [];
car[0] = {name: 'My Name'};

or this one:

var car = [];
car.push({name: 'My Name'});
like image 146
ThiefMaster Avatar answered Dec 28 '22 00:12

ThiefMaster