如何访问元素 [英] How to access the elements

查看:98
本文介绍了如何访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以按以下方式使用javascript数组吗?
如果是,我该如何访问


Can i have a javascript array in the following fashion.
If yes how can i access it


var mycars = new Array();
mycars['page1'][0] = new Array( "5.00", "5 items", "red" );
mycars['page1'][1]= new Array( "6.00", "6 items", "ed" );
mycars['page1'][2]= new Array( "7.00", "7 items", "d"

推荐答案

不,您不能通过字符串访问数组,那将是一本字典,我不认为js具有.但是2D数组很好.
No, you can''t have an array accessed by strings, that would be a dictionary, which I don''t think js has. A 2D array, however, is fine.


您可以按以下方式操作....
you can do it in the following way....
var mycars = new Array();
            mycars[0] = ["5.00", "5 items", "red"];
            mycars[1] = ["6.00", "6 items", "ed"];
            alert(mycars[0][2]);


使用两个循环,您可以遍历2d数组并找到满足您条件的元素.

using two loops you can go througth the 2d array and find the element that meets your condition.

var xlength = mycars.length;
var ylength = mycars[0].length;




您可以使用上面的代码使用两个维度的长度....




you can use the length of both dimensions using above code....


这篇关于如何访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆