简体   繁体   English

访问对象内部多维数组内的对象元素

[英]Accessing an element of an object inside a multidimensional array inside an object

I'm using knockout.js and my viewModel is an object with a multidimensional array inside it and I'm have trouble accessing a specific element. 我正在使用淘汰表.js,而我的viewModel是一个内部带有多维数组的对象,我在访问特定元素时遇到了麻烦。 My code is: 我的代码是:

var viewModel = {
    states: [
        new state("Virginia", [["Va Beach",[{lat:36.852926,lng:-75.977985}]], ["Chincoteague Island",[{lat:37.933179,lng:-75.378809}]]]),
        new state("Maryland", [["Atlantic City",[{lat:39.364283,lng:-74.422927}]], ["Ocean city",[{lat:38.336503,lng:-75.084906}]]]),
        new state("North Carolina", [["Oakacroke",[{lat:35.114615,lng:-75.98101}]], ["Nags Head",[{lat:35.957392,lng:-75.624062}]],["Emerald Isle",[{lat:34.677940,lng:-76.950776}]]])
        ]
};

var state = function(name, city) {
    this.name = name;
    this.city = ko.observableArray(city);
}

I'm trying to get the lat and lng of each individual city. 我正在尝试获取每个城市的经纬度。 I've played around with with JSON.stringify and filter but havent been able to isolate the lan and lng specifically. 我玩过JSON.stringify和filter,但是还不能专门隔离lan和lng。

http://codepen.io/ntibbs/pen/vNMKzg?editors=101 http://codepen.io/ntibbs/pen/vNMKzg?editors=101

In the context of the foreach:city binding, you would need to do something like this: foreach:city绑定的上下文中,您需要执行以下操作:

<span data-bind="text: $data[1][0].lat"></span>
<span data-bind="text: $data[1][0].lng"></span>

Basically, you states array has a property called city (bad name, it really ought to be cities ) which is, itself, an array. 基本上,你states阵列有一个属性叫做city (恶名,真的应该是cities ),这是本身的数组。 So you need to index that array (which is what foreach is doing) and each item in that array is itself an array where the first item is the name of the city and the second is an array with a single object with the lat and lng properties you need. 因此,您需要索引该数组( foreach所做的工作),并且该数组中的每个项目本身都是一个数组,其中第一个项目是城市的名称,第二个项目是一个包含有latlng的单个对象的数组您需要的属性。

So something like viewModel.states[0].city[0][1][0].lat would should give you 36.852926 因此,诸如viewModel.states[0].city[0][1][0].lat应该会给您36.852926

Of course, looking at how convoluted it is to access the properties you need, it is probably worth while to think about transforming it into something easier to work with. 当然,考虑到访问所需属性的复杂性,考虑将其转换为更易于使用的东西可能值得。

Yep, each state object has a name, and contains an array of places. 是的,每个状态对象都有一个名称,并包含一个位置数组。

Each place contains an array with a single element - a latitude, longitude pair. 每个位置都包含一个具有单个元素的数组-纬度,经度对。 eg 例如

("Virginia",   
   [  
      [
          "Va Beach",
              [
                  {lat:36.852926,lng:-75.977985}
              ]  
      ]  
   ]
)

So 所以
locationOfPlace = aState[aPlaceName][1]

And to refer to each property: 并引用每个属性:
latitudeOfPlace = aState[aPlaceName][1].lat
longitudeOfPlace = aState[aPlaceName][1].lng

You've made an odd decision to wrap the lat/lon object in an array, as if a city might have multiple coordinates attached to it. 您做出了一个奇怪的决定,将纬度/经度对象包装在一个数组中,就好像一个城市可能附加了多个坐标。

I think it would help clear up your design if, in addition to having a state object, you had a city object, rather than an object literal. 我认为,除了具有state对象之外,还具有city对象而不是对象文字,将有助于清除设计。 Something like: 就像是:

var city = function(name, lat, lon) {
    this.name = name;
    this.location = {
        lat: lat,
        lon: lon
    };
};

Then you'd declare a state more like: 然后,您将声明一个类似于以下的状态:

new state("Virginia", [
  new city("Va Beach", 36.852926, -75.977985),
  new city("Chincoteague Island", 37.933179, -75.378809)
]);

and you might access city properties like so: 您可以像这样访问城市属性:

state.city()[0].location.lat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM