簡體   English   中英

我如何從數組中獲取值

[英]How can i get the values from array

我將多個值存儲在一個數組中,例如:

var locations = [
    'Bhopal','Mobile',new google.maps.LatLng(18.40,78.81),'images/mobile.png',
    'Bangalore','Television',new google.maps.LatLng(18.30,83.90),'images/television.png',
    'Hyderabad','Footwear',new google.maps.LatLng(22.95,88.42),'images/footwear.png',
    'Kolkata','Kitchen',new google.maps.LatLng(22.58,88.33),'images/kitchen.png',
    'Mumbai','Furniture',new google.maps.LatLng(26.16,85.88),'images/furniture.png'
];

在這里我在這里得到新的latlng值

function drop() {
  clearMarkers();
  for (var i = 0; i < locations.length; i++) {
    addMarkerWithTimeout(locations[i], i * 200);
  }
}

我需要的是,drop函數是從數組獲取LatLng,我還需要獲取前兩個值,例如'bhopal'和'mobile'並發出警報。 我該怎么辦?

function drop() {
  clearMarkers();
  for (var i = 0; i < locations.length / 4; i++) {
    // locations[2 + 4*i]  = latlng value  google.maps, ....
    // locations[4*i]  =  first column, Bhopal, Bangalore, ...
    // locations[1+4*i] = second column, Mobile, Television, ...
    // locations[3+4*i] = last column image
  }
}

如我所見,您將數據保留在怪異的表示中。 最好保持這種格式:

var locations = [
    {location: 'Bhopal', type: 'Mobile', latlng: new google.maps.LatLng(18.40,78.81), iamge: 'images/mobile.png'},
    {location: 'Bhopal', type: 'Mobile', latlng: new google.maps.LatLng(18.40,78.81), iamge: 'images/mobile.png'},
    {location: 'Bhopal', type: 'Mobile', latlng: new google.maps.LatLng(18.40,78.81), iamge: 'images/mobile.png'},
    {location: 'Bhopal', type: 'Mobile', latlng: new google.maps.LatLng(18.40,78.81), iamge: 'images/mobile.png'},
];

但是,如果您想保留格式(或者無法修改格式),則可以執行以下操作:

for (var i = 0; i < locations.length; i+=4) {
     alert(locations[i]);
     alert(locations[i+1]);
 }

首先,您的數據結構似乎不符合您想要的目的。

您需要一個關聯數組,而不是普通數組。

一個粗略的例子:

var locations = [
    {"city":"Bhopal","type":"Mobile","latitude":123491283},
    {"city":"Paris","type":"SomethingElse","latitude":2342342},
    {"city":"Milano","type":"Landline","latitude":56456545}
]

基本上,數組中的“對象”。

現在您可以像這樣訪問它:

 for (var i = 0; i < locations.length; i++) {
     var city = locations[i].city;
     var city = locations[i].type;
     var latitude = locations[i].latitude;
 }

不是你想要的,但是嘿..

嘗試:

var locations = [
    ['Bhopal','Mobile',new google.maps.LatLng(18.40,78.81),'images/mobile.png'],
    ['Bangalore','Television',new google.maps.LatLng(18.30,83.90),'images/television.png'],
    ['Hyderabad','Footwear',new google.maps.LatLng(22.95,88.42),'images/footwear.png'],
    ['Kolkata','Kitchen',new google.maps.LatLng(22.58,88.33),'images/kitchen.png'],
    ['Mumbai','Furniture',new google.maps.LatLng(26.16,85.88),'images/furniture.png']
];


function drop() {
  clearMarkers();
  for (var i = 0; i < locations.length; i++) {
    addMarkerWithTimeout(locations[i][2], i * 200);
  }
}

這就是第一行的處理方式。

 for (var i = 0; i < locations.length; i=i+4) {
     locations[i] has 'Bhopal'
     locations[i+1] has 'Mobile'
     locations[i+2] has the object
     locations[i+3] has 'images/mobile.png'
 }

在您的代碼中相應地使用它們。

為什么不這樣創建數組:

var locations = [
    {Place : 'Bhopal', Thing : 'Mobile', Location : new google.maps.LatLng(18.40,78.81), Image : 'images/mobile.png'},
    {Place : 'Bangalore',Thing : 'Television',Location : new google.maps.LatLng(18.30,83.90),Image : 'images/television.png'},
    {Place : 'Hyderabad',Thing : 'Footwear',Location : new google.maps.LatLng(22.95,88.42),Image : 'images/footwear.png'}];

以這種方式管理它更容易。

如果您打算使數據保持問題的格式,那么建議您創建一個用於訪問記錄的function ,該function基於每個記錄的長度為4個值,這樣您可以一次又一次地使用它。

這是我在說的一個例子

 Number.MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; function getRecord(data, recNum) { var length; if (!Array.isArray(data)) { throw new Error('data is not an Array'); } length = data.length; if (length < 4 || length % 4 !== 0) { throw new Error('data is empty or record length is incorrect'); } if (typeof recNum !== 'number' || recNum < 0 || recNum > Math.floor(Number.MAX_SAFE_INTEGER / 4)) { throw new Error('recNum is out of range or not a valid number'); } return data.slice(recNum, recNum + 4); } var pre = document.getElementById('out'), locations = [ 'Bhopal', 'Mobile', { lat: 0, lng: 0 }, 'images/mobile.png', 'Bangalore', 'Television', { lat: 0, lng: 0 }, 'images/television.png', 'Hyderabad', 'Footwear', { lat: 0, lng: 0 }, 'images/footwear.png', 'Kolkata', 'Kitchen', { lat: 0, lng: 0 }, 'images/kitchen.png', 'Mumbai', 'Furniture', { lat: 0, lng: 0 }, 'images/furniture.png' ], length = Math.floor(locations.length / 4), index; for (index = 0; index < length; index += 1) { pre.textContent += JSON.stringify(getRecord(locations, index), null, 2) + '\\n\\n'; } 
 <pre id="out"></pre> 

因此,調用getRecord(locations, 0)將返回長度為4的數組和以下數據

[
  "Bhopal",
  "Mobile",
  {
    "lat": 0,
    "lng": 0
  },
  "images/mobile.png"
]

現在,您可以訪問該數組,其中

索引[0]Bhopal

索引[3]images/mobile.png

將數據重新排列(源轉換或一次性轉換)為更適合您需要的結構的替代方法。

可能更合適的結構是

數組數組

[
    [...data0],
    [...dataN]
]

對象數組

[
    {...data0},
    {...dataN}
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM