【问题标题】:JS array not returning index values of specific text even though text exists即使文本存在,JS数组也不返回特定文本的索引值
【发布时间】:2021-07-10 19:27:09
【问题描述】:

对 JS 相当陌生。我有一个 HTML 表格,其中的列按以下顺序排列:大陆、国家等... 到目前为止,我的工作代码可以读取 HTML 表格,为所有国家/地区创建一个数组,然后删除重复项。我还有一个数组,可以在 HTML 表中为国家数组的每个元素查找大陆。

我的问题是我似乎无法在大洲数组中查找某个值的所有索引(例如“非洲”)。这是我的代码:

//Step 1: create an array to hold each country's continent. The 'countries' array was 
previously created. 
 
   var continents = [];
   for (const element of countries){
     console.log(element);

      var contin = []; 
      for (j = 0 ; j < data.rows.length; j++){
         if (data.rows[j].cells[1].innerHTML.indexOf(element) >= 0) 
         contin.push(data.rows[j].cells[0].innerHTML);
         }
         continents.push(contin);
}

//Step 2: Create a function that can look up all indexes in the continents array for a certain value.
        function getAllIndexes(arr, val) {
          var indexes = [], i;
          for(i = 0; i < arr.length; i++)
              if (arr[i] === val)
              indexes.push(i);
          return indexes;
        }

//Step 3: Run the function to get all index values where continents array = "Africa"
         indexesAfrica = getAllIndexes(continents, "Africa");
         alert(indexesAfrica);

当我运行该函数时,它返回一个空白。我已经在其他数组上测试了该函数并且它可以工作。例如,此代码返回“1,3”的正确索引:

var funArr = ["test", "Africa", "Hello","Africa"];
indexesAfrica = getAllIndexes(funArr, "Africa");
alert(indexesAfrica);

这让我觉得我的大洲数组有问题?当我提醒(大陆)时,大陆数组显示正确的数组项列表,但我似乎无法在其中查找任何内容。想法?

【问题讨论】:

  • 您需要给我们continents数组数据。最好创建一个包含结果和/或任何错误的完整示例
  • 分享这个最简单的方法?我的 HTML 表格很长
  • 好的,所以我从控制台看到,与国家/地区数组相比,我的大洲数组中的数据看起来不同。在控制台中查看“国家”如下所示: 0:“阿尔及利亚”1:“安哥拉”2:“贝宁”3:“博茨瓦纳”4:“布基纳法索”5:“布隆迪”6:“喀麦隆”7:“开普敦” Verde”等...而“大陆”看起来像这样:0:(3)[“Africa”,“Africa”,“Africa”] 1:[“Africa”] 2:(3)[“Africa”,“非洲”、“非洲”] 3: (3) [“非洲”、“非洲”、“非洲”] 4: (2) [“非洲”、“非洲”] 5: [“非洲”] 6: (4 ) ["Africa", "Africa", "Africa", "Africa"] 7: ["Africa"] 等....
  • 所以我想我明白发生了什么。我的大洲数组实际上是一个数组数组,而不是单个数组。将继续调查。

标签: javascript arrays indexing lookup


【解决方案1】:

我打算将此作为评论留下,但它似乎可以解决你的情况,也许有助于教一些东西。为什么将它们作为 2 个单独的数组?它的可扩展性不是很高。如果您对国家/地区数组进行排序,您将失去与大陆的关联。最好将它们组合成一个易于过滤的对象数组(例如,如果您只想显示位于非洲的国家/地区)。

let countries = ["Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde", "USA"]
let data = document.querySelector('table');

let globe = countries.map(country => { // using map, we can transform each incoming country into a country/continent object
  let continent = 'N/A'; // just so we have somethign in case we dont find a continent
  [...data.rows].forEach(row => { // data is the table, rows are the <tr> tags, but we need to make it iterable for the loop, so we wrap it in a spread [...] 
    if (row.cells[1].innerText.trim() === country) continent = row.cells[0].innerText.trim();
  })
  return { country: country, continent: continent}; // we return an object back to our map
});
console.log(globe)

//Step 2: Run the function to get all index values where continents array = "Africa"

const africanCountries = globe.filter(e => e.continent === "Africa").map(e => e.country);
console.log(africanCountries)
<table>
  <tr><td>Africa</td><td>Algeria</td></tr>
  <tr><td>Africa</td><td>Angola</td></tr>
  <tr><td>Africa</td><td>Benin</td></tr>
  <tr><td>North America</td><td>USA</td></tr>
  <tr><td>Africa</td><td>Botswana</td></tr>
  <tr><td>Africa</td><td>Burkina Faso</td></tr>
  <tr><td>Africa</td><td>Faso</td></tr>
  <tr><td>Africa</td><td>Burundi</td></tr>
  <tr><td>Africa</td><td>Cameroon</td></tr>
  <tr><td>Africa</td><td>Cape Verde</td></tr>
</table>

更新:要添加纬度和经度,您可以在 map() 中点击索引

let globe = countries.map((country, index) => { // using map, we can transform each incoming country into a country/continent object
  let continent = 'N/A'; // just so we have somethign in case we dont find a continent
  [...data.rows].forEach(row => { // data is the table, rows are the <tr> tags, but we need to make it iterable for the loop, so we wrap it in a spread [...] 
    if (row.cells[1].innerText.trim() === country) continent = row.cells[0].innerText.trim();
  })
  return { 
     country: country, 
     continent: continent,
     lat: countryLat[index],
     long: countryLong[index]
   }; // we return an object back to our map
});

【讨论】:

  • 好建议!我得到了这个工作;这个想法对我接下来要做的事情更有意义。我有每个国家独有的三个附加变量,将其存储在这个对象数组中是有意义的:纬度、经度和平均温度。目前,这些值都在它们自己的数组中(存储顺序与“国家”相同),称为:“countryLat”、“countryLong”和“countryTemp”我如何将这 3 个数组中的所有值推送到您的每个国家/地区的“全球”数组?
  • 该数据是否与国家和大陆在同一个表中?
  • 没有。由于某些国家/地区有多个条目,因此我必须对每个国家/地区的纬度、经度和平均温度进行一些计算。所以到目前为止,我有 4 个数组:'countries'、'countryLat'、'countryLong' 和 'countryTemp'。每个数组的长度和顺序相同(例如,如果阿尔及利亚在 'countries' 中的索引号为 3,则其他三个数组中的相应索引号也为 3)。
  • 好吧,看来我想通了。我刚刚循环并添加了一个这样的新属性:for (j = 0 ; j &lt; globe.length; j++){ globe[j].lat = countryLat[j]; }
  • @carvalh3 - 是的,但您也可以使用原始的 map() 函数索引。查看修改后的答案 - 最后一部分
猜你喜欢
  • 2016-11-03
  • 2019-08-08
  • 2020-07-27
  • 2019-10-25
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
相关资源
最近更新 更多