访问对象内的数组
Access array inside an object
var b = {
maths:[12,23,45],
physics:[12,23,45],
chemistry:[12,23,45]
};
我想访问对象 b 中的数组,即数学、物理、化学。这可能是一个简单的问题,但我正在学习....谢谢
给定对象 b 中的数组(请注意,您提供的代码中存在语法错误(
var b = {
maths: [12, 23, 45],
physics: [12, 23, 45],
chemistry: [12, 23, 45]
};
maths
、physics
和chemistry
被称为存储在变量b
中的对象的properties
您可以使用点表示法访问对象的属性:
b.maths[0]; //get first item array stored in property maths of object b
访问对象属性的另一种方法是:
b['maths'][0]; //get first item array stored in property maths of object b
var b = {
maths:[12,23,45],
physics:[12,23,45],
chemistry:[12,23,45]
};
console.log(b.maths);
// or
console.log(b["maths"]);
// and
console.log(b.maths[0]); // first array item
var b = {
maths:[12,23,45],
physics:[12,23,45],
chemistry:[12,23,45]
};
// using loops you can do like
for(var i=0;i<b.maths.length;i++){
console.log(b.maths[i]);//will give all the elements
}
有
很简单:
b = {
maths:[12,23,45],
physics:[12,23,45],
chemistry:[12,23,45]
};
b.maths[1] // second element of maths
b.physics
b.chemistry
你需要像这样设置变量 b:
var b = {
maths:[12,23,45],
physics:[12,23,45],
chemistry:[12,23,45]
};
然后,您可以使用 b.maths、b.physics 和 b.chemistry 访问 b 中的数组。
如果您需要访问数组中的数组,请尝试此操作。
var array2 = ["Bannana", ["Apple", ["Orange"], "Blueberries"]];
array2[1, 1, 0];
console.log(array2[1][1][0]);
在这里,我说的是进入最内部的数组并拉动 0 中的位置。
相关文章:
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象