使用字符串来定位对象
我对 javascript 比较陌生。我正在使用appcelerator的titanium SDK来创建一个移动应用程序,它只使用javascript。
不管怎样,我正在创建一个层次结构菜单系统。我有充满子菜单项的数据数组。每个数据数组都填充有不同的子菜单。当我单击菜单项时,它会收到一个变量,让应用程序知道接下来要加载哪个数据数组。这是一个基本的例子:
var data = [
{title:'Item 1',subID:'1', hasDetail:true,},
{title:'Item 2',subID:'1', hasDetail:true,}
];
var data1 = [
{title:'Child1',subID:'1_1', hasDetail:true},
{title:'Child2', subID:'1_2', hasDetail: true}
];
var data1_1 = [
{title:'Grandchild1'},
{title:'Grandchild2'}
];
// I have a ton of menu items though, so the method I'm using
// below is very inefficient:
if(win.subID == '1'){
tableview.setData(data1);
} else if(win.subID == '2'){
tableview.setData(data2);
}
还有很多菜单(1,2,3,4等和1_1,1_2,1_3,2_1,2_2等),所以上面设置数据的方法效率极低。我想要的是这样的:
tableview.setData(data + win.subID);
但是,我知道 data 是一个数组对象,而 subID 是一个字符串。如何将对象的名称与字符串组合,然后将其转换为再次引用对象?
谢谢,如果以上有任何不清楚的地方,请告诉我。我已经为此苦苦挣扎了很长时间。
编辑:
这是我需要的换句话说:
我有一个字符串,我想用它来引用数组。
data1 = [
{value: 1},
{value: 2}
]
var string = 'data1';
magicStringToArrayFunction(string);
tableview.setData(string);
我需要的是神奇的字符串到数组/对象函数。
I'm relatively new to javascript. I'm using appcelerator's titanium SDK to create a mobile app though, and it uses only javascript.
Anyways, I have a hierarchy menu system that I'm creating. I have data arrays filled with submenu items. Each data array is filled with a different submenu. When I click on a menu item, it receives a variable that lets the application know which data array to load next. Here is a basic example:
var data = [
{title:'Item 1',subID:'1', hasDetail:true,},
{title:'Item 2',subID:'1', hasDetail:true,}
];
var data1 = [
{title:'Child1',subID:'1_1', hasDetail:true},
{title:'Child2', subID:'1_2', hasDetail: true}
];
var data1_1 = [
{title:'Grandchild1'},
{title:'Grandchild2'}
];
// I have a ton of menu items though, so the method I'm using
// below is very inefficient:
if(win.subID == '1'){
tableview.setData(data1);
} else if(win.subID == '2'){
tableview.setData(data2);
}
There are many more menus (1,2,3,4,etc and 1_1,1_2,1_3,2_1,2_2, etc), so the method above of setting the data is extremely inefficient. What I'd like to so is something like this:
tableview.setData(data + win.subID);
However, I know that data is an array object and subID is a string. How can I combine an object's name with a string, then convert it to refer to an object again?
Thanks, and please let me know if anything above was unclear. I've been struggling with this for a really long time.
EDIT:
Here's what I need in other word's:
I have a string, which I want to use to reference an array.
data1 = [
{value: 1},
{value: 2}
]
var string = 'data1';
magicStringToArrayFunction(string);
tableview.setData(string);
What I need is that magical string to array/object function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对钛的了解不够,无法直接回答这个问题,但这里有一个提示。钛中的全球物体是什么?你知道,如果你声明了一个全局变量,那么这个变量属于什么对象呢?
在浏览器环境中,全局对象是window。使用全局变量或者调用全局函数实际上就是访问window对象的属性和调用方法。因此,在网络浏览器中:
因此
,在您的情况下,如果这是常规网络浏览器,您的代码将是:
不过,并非所有 javascript 实现都以这种方式工作,这适用于所有支持 javascript 的已知网络浏览器。不知道钛合金是否可以做到这一点。
I don't know titanium enough to answer this directly but here's a hint. What's the global object in titanium? You know, if you declare a global variable, what object does that variable belong to?
In a browser environment, the global object is window. Using a global variable or calling a global function is actually accessing properties and calling methods of the window object. So, in a web browser:
and
So in your case, if this were a regular web browser, your code would be:
Not all javascript implementations work this way though, this works on all known web browsers with javascript support. Don't know if you can do this in titanium.
我认为你想要的是一个数组数组,所以你可以这样做......
I think what you want is an array of arrays so you can do this...
可能有一种更优雅的方法来做到这一点,但我累了:]
但是你可以使用哈希表作为解决方案
Javascript 哈希表
类似的东西可能会成功。
There is probably a more elegant way of doing this but I'm tired :]
But you could use hash tables as a solution
Javascript hash tables
Something like that may do the trick.