【问题标题】:Unable to make array of arrays in JS无法在 JS 中创建数组数组
【发布时间】:2017-03-20 09:39:23
【问题描述】:

我需要制作一个像

这样的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 循环等。

请告诉我我做错了什么?

【问题讨论】:

  • searchLocations 定义在哪里?
  • searchLocations 正在从 db 结果返回,可以成功调用为searchResults[i].namesearchResults[i].latitudesearchResults[i].longtitudesearchResults[i].zm。它只是运行console.log(searchLocations.length);0
  • 您可以编辑您的问题并发布搜索结果吗?
  • 我可以做一个简单的 console.log(searchResults);结果类似于&gt;[Object, Object, Object, Object] &gt;0:Object &gt;1:Object name:"Duhok" latitude:"36.700016" longtitude:"43.9204" zm:"4address" pho: "" &gt;__prototype__: Object 等。

标签: javascript arrays jagged-arrays


【解决方案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)
}

【讨论】:

  • 这和我的问题有点像,而且我试过了,但也没有用。
  • @MShoaibQureshi 我已经尝试使用您提供的演示数据,它完全可以工作。
【解决方案2】:

这样试试

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

【讨论】:

  • 使用了这个,但这也为console.log(searchLocations.length);提供了零长度
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      问题似乎在于您的 searchLocations 的长度属性为 0,并且您无法通过 for (i = 0; i &lt; 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)将其转为数组,然后循环遍历。

      【讨论】:

        【解决方案6】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2015-11-14
          • 2019-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-09
          • 1970-01-01
          • 2023-03-12
          • 2014-07-28
          相关资源
          最近更新 更多