最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Accessing Javascript variable stored in an array - Stack Overflow

programmeradmin2浏览0评论

I currently have an array as follows:

var allMenus = ['vehicleMakeFb','vehicleModelFb','vehicleTrimFb']

Where vehicleMakeFb etc. are actually objects. I originally tried to store the objects in the array, but I understand this is not possible in JS, so I have resorted to storing the names of the variables as strings.

My intention is to be able to iterate through the variables, and get individual properties of each, such as:

for (fbMenu in allMenus) {
    alert(vehicleMakeFb.divId);
}

Where divId is a string.

What is the correct syntax to access this variable by its name?

I currently have an array as follows:

var allMenus = ['vehicleMakeFb','vehicleModelFb','vehicleTrimFb']

Where vehicleMakeFb etc. are actually objects. I originally tried to store the objects in the array, but I understand this is not possible in JS, so I have resorted to storing the names of the variables as strings.

My intention is to be able to iterate through the variables, and get individual properties of each, such as:

for (fbMenu in allMenus) {
    alert(vehicleMakeFb.divId);
}

Where divId is a string.

What is the correct syntax to access this variable by its name?

Share Improve this question asked Jun 23, 2011 at 17:25 HarryHarry 4,8468 gold badges42 silver badges70 bronze badges 2
  • 1 it is entirely possible to store objects in an array. – thescientist Commented Jun 23, 2011 at 17:27
  • Who told you that it's impossible to use JS to store objects in an array? – Sparky Commented Jun 23, 2011 at 17:37
Add a ment  | 

3 Answers 3

Reset to default 3

What do you mean you can't store an array of objects?

var allMenus = [
  {make: 'Ford', model: 'Mustang', trim: 'Coupe'},
  {make: 'Ford', model: 'Explorer', trim: 'Eddie Bauer'}
];

// all items
for (var i = 0; i < allMenus.length; i++){
  var fbMenu = allMenus[i];

  // explicit list of properties
  console.log('[X] Make: ' + fbMenu.make);
  console.log('[X] Model: ' + fbMenu.model);
  console.log('[X] Trim: ' + fbMenu.trim);

  // discovered list of properties
  for (var prop in fbMenu){
    console.log('[D] ' + prop + ': ' + fbMenu[prop]);
  }
}

You can access object properties in two ways:

Dot Notation

object.prop

Brackets

object['prop']

You can store objects in an array.

var arrayFullOfObjects = [{
   foo: 'bar'
},
  window,
  jQuery
];

arrayFullOfObjects[0].foo; //bar

You can access the value of a property of a JS object using any of the following syntax:

// assuming the following object
var pany = { employee : { name : 'foo' }  };

// we can access the name property via
var name1 = pany.employee.name;
var name2 = pany.employee['name'];
var name3 = pany['employee']['name'];
// ...

You can then store objects into arrays in much the same way:

var obj1, obj2, obj3; // some objects (initialized prior)
var myArray = [obj1, obj2, obj3];

Looking at your code, just make sure you ditch the quotes on your array JSON. (i.e. [obj], not ['obj']).

发布评论

评论列表(0)

  1. 暂无评论