2017-03-20 26 views
0

我需要做一個JS數組一樣無法讓數組的數組在JS

var locations = [ 
      ['Erbil, Soran POS', 36.649415, 44.534143, 4], 
      ['Duhok, Duhok', 36.867905, 42.948857, 4], 
      ['Duhok, Akre', 36.700016, 43.920457, 4], 
      ['Duhok, Zakho', 37.150462, 42.672677, 4], 
      ['Duhok, Shiladze', 36.650648, 44.519263, 4], 
      ['Duhok, Bardaresh', 36.650648, 44.519263, 4], 
      ['Mosel, Sinjar', 36.314216, 41.862443, 4], 
      ['Duhok, Duhok', 36.867905, 42.948857, 4], 
      ['Duhok, Akre', 36.741121, 43.880849, 4], 
      ['Duhok, Zakho', 37.150462, 42.672677, 4], 
      ['Duhok, Shiladze', 36.650648, 44.519263, 4], 
      ['Duhok, Bardaresh', 36.911505, 42.728491, 4], 
      ['Duhok, Amadia', 37.091727, 43.487692, 4], 
      ['Sulimanya, Sulimanya', 35.557045, 45.435943, 4], 
      ['Sulimanya, Dirbendikan', 35.110939, 45.695271, 4], 
    ]; 

我想是這樣的:

var locations; 
for (i = 0; i < searchResults.length; i++) { 
    locations = new Array(searchResults[i].name, searchResults[i].latitude, searchResults[i].longtitude, searchResults[i].zm); 
    searchLocations.push(locations) 
} 

但它的製造具有數據,但其長度數組是'0',因此我不能用於循環等。

請告訴我在做什麼錯?

+0

「searchLocations」的定義在哪裏? – abhishekkannojia

+0

'searchLocations'正在從db結果返回,並且可以成功調用爲searchResults [i] .name,'searchResults [i] .latitude','searchResults [i]。longitude','searchResults [i] .zm'。 它只是運行'console.log(searchLocations.length);'給出'0'。 –

+0

你能編輯你的問題併發布searchResults嗎? –

回答

0

隨着一些命中和試驗,我設法通過以下方式。

searchResults = data.data;//data from DB 
//console.log(searchResults); 
var locations = new Array(); 

for (i = 0; i < searchResults.length; i++) { 
locations[i] = [searchResults[i].name, searchResults[i].latitude, searchResults[i].longtitude, searchResults[i].zm]; 
} 

這使完全相同的陣列就像我需要

[ 
     ['Duhok, Duhok', 36.867905, 42.948857, 4], 
     ['Sulimanya, Sulimanya', 35.557045, 45.435943, 4], 
     ['Sulimanya, Dirbendikan', 35.110939, 45.695271, 4], 
]; 

希望這有助於別人誰也陷入這樣那樣的問題。

1

嘗試用以下

var searchLocations=[]; 
for (i = 0; i < searchResults.length; i++) { 
    var locations = new Array(searchResults[i].name, searchResults[i].latitude, searchResults[i].longtitude, searchResults[i].zm); 
    searchLocations.push(locations) 
} 
+0

這與我的問題有點相同,而且我已經嘗試過,但它也不起作用。 –

+0

@ MShoaibQureshi我已經嘗試使用您提供的演示數據,它的工作完全。 –

0

嘗試這樣

for (i = 0; i < searchResults.length; i++) { 
    var locations = []; 
    locations.push(searchResults[i].name); 
    location.push(searchResults[i].latitude); 
    location.push(searchResults[i].longtitude); 
    location.push(searchResults[i].zm) 
    searchLocations.push(locations) 
} 
+0

使用這個,但是這也給了'console.log(searchLocations.length);零長度' –

0

您可以使用Array.prototype.map遍歷數組的每個項目,創建與返回值的新陣列。

我們還使用對象解構來將對象的按鍵從回調函數中創建局部變量的對象中移出。

ES6

const searchResults = [ 
 
    { name: 'Erbil, Soran POS', latitude: 36.649415, longitude: 44.534143, zm: 4 }, 
 
    { name: 'Duhok, Duhok', latitude: 36.867905, longitude: 42.948857, zm: 4 }, 
 
    { name: 'Duhok, Akre', latitude: 36.700016, longitude: 43.920457, zm: 4 }, 
 
] 
 

 
const searchLocations = searchResults.map(({ name, latitude, longitude, zm }) => ([ 
 
    name, 
 
    latitude, 
 
    longitude, 
 
    zm 
 
])) 
 

 
console.log(searchLocations)

ES5

var searchResults = [ 
 
    { name: 'Erbil, Soran POS', latitude: 36.649415, longitude: 44.534143, zm: 4 }, 
 
    { name: 'Duhok, Duhok', latitude: 36.867905, longitude: 42.948857, zm: 4 }, 
 
    { name: 'Duhok, Akre', latitude: 36.700016, longitude: 43.920457, zm: 4 } 
 
] 
 

 
var searchLocations = searchResults.map(function(location) { 
 
    return [ 
 
    location.name, 
 
    location.latitude, 
 
    location.longitude, 
 
    location.zm 
 
    ] 
 
}) 
 
    
 
console.log(searchLocations)

0

在仔細閱讀你的問題後,我得出結論,這可能是你期望的。

var locations = [['Erbil, Soran POS', 36.649415, 44.534143, 4], ['Duhok, Duhok', 36.867905, 42.948857, 4], ['Duhok, Akre', 36.700016, 43.920457, 4], ['Duhok, Zakho', 37.150462, 42.672677, 4], ['Duhok, Shiladze', 36.650648, 44.519263, 4], ['Duhok, Bardaresh', 36.650648, 44.519263, 4], ['Mosel, Sinjar', 36.314216, 41.862443, 4], ['Duhok, Duhok', 36.867905, 42.948857, 4], ['Duhok, Akre', 36.741121, 43.880849, 4], ['Duhok, Zakho', 37.150462, 42.672677, 4], ['Duhok, Shiladze', 36.650648, 44.519263, 4], ['Duhok, Bardaresh', 36.911505, 42.728491, 4], ['Duhok, Amadia', 37.091727, 43.487692, 4], ['Sulimanya, Sulimanya', 35.557045, 45.435943, 4], ['Sulimanya, Dirbendikan', 35.110939, 45.695271, 4]], 
 
    searchResults = [], i = 0, elems = ['name', 'latitude', 'longitude', 'zm']; 
 

 
    while (i < 4){ 
 
     var obj = {}, arr = []; 
 
     locations.forEach(function(v){ 
 
     arr.push(v[i]) 
 
     }); 
 
     obj[elems[i]] = arr; 
 
     searchResults.push(obj); 
 
     i++ 
 
    } 
 
    console.log(searchResults);

0

看來問題是,你的searchLocations有length屬性爲0,則無法通過其循環由for (i = 0; i < i.length; i++)因爲條件計算結果始終爲false。但是,您可以通過索引訪問searchLocations的項目。

基於此我猜searchLocations是一個'類似數組的對象'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Array-like_objects,但不是本地數組。您可以嘗試通過searchLocationsArray = Array.prototype.call(searchLocations)將其轉換爲數組,然後在其中循環。