[英]Javascript array with a mix of literals and arrays
I can create the following and reference it using 我可以创建以下内容并使用它来引用它
area[0].states[0]
area[0].cities[0]
var area = [
{
"State" : "Texas",
"Cities" : ['Austin','Dallas','San Antonio']
},
{
"State" :"Arkansas",
"Cities" : ['Little Rock','Texarkana','Hot Springs']
}
] ;
How could I restructure "area" so that if I know the name of the state, I can use it in a reference to get the array of cities? 我怎么能重组“区域”,这样如果我知道州的名称,我可以在参考中使用它来获得城市阵列?
Thanks 谢谢
EDIT Attempting to implement with the answers I received (thanks @Eli Courtwright, @17 of 26, and @JasonBunting) I realize my question was incomplete. 编辑尝试实施我收到的答案(感谢@Eli Courtwright,@ 17 of 26和@JasonBunting)我意识到我的问题不完整。 I need to loop through "area" the first time referencing "state" by index, then when I have the selection of the "state", I need to loop back through a structure using the value of "state" to get the associated "cities".
我需要在第一次通过索引引用“状态”时循环“区域”,然后当我选择“状态”时,我需要使用“state”的值循环回一个结构来获取相关的“城市”。 I do want to start with the above structure (although I am free to build it how I want) and I don't mind a conversion similar to @eli's answer (although I was not able to get that conversion to work).
我确实想从上面的结构开始(虽然我可以自由地构建它我想要的)并且我不介意类似于@eli的答案的转换(尽管我无法使转换工作)。 Should have been more complete in first question.
第一个问题应该更完整。 Trying to implement 2 select boxes where the selection from the first populates the second...I will load this array structure in a js file when the page loads.
尝试实现2个选择框,其中第一个选择填充第二个...我将在页面加载时将此数组结构加载到js文件中。
var area =
{
"Texas" : { "Cities" : ['Austin','Dallas','San Antonio'] },
"Arkansas" : { "Cities" : ['Little Rock','Texarkana','Hot Springs'] }
};
Then you can do: 然后你可以这样做:
area["Texas"].Cities[0];
(With help from the answers, I got this to work like I wanted. I fixed the syntax in selected answer, in the below code) (在答案的帮助下,我按照我想要的方式工作。我修改了选定答案的语法,在下面的代码中)
With the following select boxes 使用以下选择框
<select id="states" size="2"></select>
<select id="cities" size="3"></select>
and data in this format (either in .js file or received as JSON) 和这种格式的数据(在.js文件中或作为JSON接收)
var area = [
{
"states" : "Texas",
"cities" : ['Austin','Dallas','San Antonio']
},
{
"states" :"Arkansas",
"cities" : ['Little Rock','Texarkana','Hot Springs']
}
] ;
These JQuery functions will populate the city select box based on the state select box selection 这些JQuery函数将根据状态选择框选择填充城市选择框
$(function() { // create an array to be referenced by state name
state = [] ;
for(var i=0; i<area.length; i++) {
state[area[i].states] = area[i].cities ;
}
});
$(function() {
// populate states select box
var options = '' ;
for (var i = 0; i < area.length; i++) {
options += '<option value="' + area[i].states + '">' + area[i].states + '</option>';
}
$("#states").html(options); // populate select box with array
// selecting state (change) will populate cities select box
$("#states").bind("change",
function() {
$("#cities").children().remove() ; // clear select box
var options = '' ;
for (var i = 0; i < state[this.value].length; i++) {
options += '<option value="' + state[this.value][i] + '">' + state[this.value][i] + '</option>';
}
$("#cities").html(options); // populate select box with array
} // bind function end
); // bind end
});
If you want to just create it that way to begin with, just say 如果你想以这种方式创建它,那就说吧
area = {
"Texas": ['Austin','Dallas','San Antonio']
}
and so on. 等等。 If you're asking how to take an existing object and convert it into this, just say
如果您正在询问如何获取现有对象并将其转换为此对象,请说明
states = {}
for(var j=0; j<area.length; j++)
states[ area[0].State ] = area[0].Cities
After running the above code, you could say 运行上面的代码后,你可以说
states["Texas"]
which would return 哪会回来
['Austin','Dallas','San Antonio']
This would give you the array of cities based on knowing the state's name: 这将根据了解州名称为您提供一系列城市:
var area = {
"Texas" : ["Austin","Dallas","San Antonio"],
"Arkansas" : ["Little Rock","Texarkana","Hot Springs"]
};
// area["Texas"] would return ["Austin","Dallas","San Antonio"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.