js数组不返回特定文本的索引值,即使文本存在

neekobn8  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(485)

对js来说相当陌生。我有一个html表格,列的顺序是:大陆,国家,等等。。。到目前为止,我已经有了工作代码,可以读取html表,为所有国家创建一个数组,然后删除重复项。我还有一个数组,它在html表中为countries数组的每个元素查找大陆。
我的问题是,我似乎无法查找大陆数组中的所有索引以获得某个值(例如“非洲”)。这是我的密码:

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

这让我觉得我的大陆阵列有问题?当我发出警报(大陆)时,大陆阵列显示正确的阵列项目列表,但我似乎无法在其中查找任何内容。思想?

cotxawn7

cotxawn71#

我本打算把这作为一个评论,但它似乎可以解决你的情况,也许可以帮助你学到一些东西。为什么将它们作为两个独立的阵列?它的伸缩性不是很好。如果你对你的国家阵列进行排序,你将失去与欧洲大陆的联系。最好将它们耦合在一组对象中,这样很容易过滤(比如,如果您只想显示非洲的国家)。

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>

更新:要添加lat和long,可以点击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
});

相关问题