无法在JS中创建数组数组

Unable to make array of arrays in 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”的数组,因此我不能使用 for 循环等。

请告诉我我做错了什么?

尝试以下

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)
}

这样试试

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)
}

您可以使用 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)

在仔细阅读您的问题后,我得出结论,这可能是您所期望的。

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);

问题似乎在于您的 searchLocations 的长度 属性 为 0,并且您无法按 for (i = 0; i < i.length; i++) 遍历它,因为该条件的计算结果始终为假。但是,您可以通过索引访问 searchLocations 的项目。

基于此,我猜测 searchLocations 是 'Array-like Object' https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Array-like_objects,但不是原生数组。您可以尝试通过searchLocationsArray = Array.prototype.call(searchLocations)将其转换为数组,然后循环遍历它。

经过一些打击和尝试,我通过以下方式做到了这一点。

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],
];

希望这对遇到此类问题的其他人也有帮助。