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

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

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

  1. //Step 1: create an array to hold each country's continent. The 'countries' array was
  2. previously created.
  3. var continents = [];
  4. for (const element of countries){
  5. console.log(element);
  6. var contin = [];
  7. for (j = 0 ; j < data.rows.length; j++){
  8. if (data.rows[j].cells[1].innerHTML.indexOf(element) >= 0)
  9. contin.push(data.rows[j].cells[0].innerHTML);
  10. }
  11. continents.push(contin);
  12. }
  13. //Step 2: Create a function that can look up all indexes in the continents array for a certain value.
  14. function getAllIndexes(arr, val) {
  15. var indexes = [], i;
  16. for(i = 0; i < arr.length; i++)
  17. if (arr[i] === val)
  18. indexes.push(i);
  19. return indexes;
  20. }
  21. //Step 3: Run the function to get all index values where continents array = "Africa"
  22. indexesAfrica = getAllIndexes(continents, "Africa");
  23. alert(indexesAfrica);

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

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

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

cotxawn7

cotxawn71#

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

  1. let countries = ["Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde", "USA"]
  2. let data = document.querySelector('table');
  3. let globe = countries.map(country => { // using map, we can transform each incoming country into a country/continent object
  4. let continent = 'N/A'; // just so we have somethign in case we dont find a continent
  5. [...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 [...]
  6. if (row.cells[1].innerText.trim() === country) continent = row.cells[0].innerText.trim();
  7. })
  8. return { country: country, continent: continent}; // we return an object back to our map
  9. });
  10. console.log(globe)
  11. //Step 2: Run the function to get all index values where continents array = "Africa"
  12. const africanCountries = globe.filter(e => e.continent === "Africa").map(e => e.country);
  13. console.log(africanCountries)
  1. <table>
  2. <tr><td>Africa</td><td>Algeria</td></tr>
  3. <tr><td>Africa</td><td>Angola</td></tr>
  4. <tr><td>Africa</td><td>Benin</td></tr>
  5. <tr><td>North America</td><td>USA</td></tr>
  6. <tr><td>Africa</td><td>Botswana</td></tr>
  7. <tr><td>Africa</td><td>Burkina Faso</td></tr>
  8. <tr><td>Africa</td><td>Faso</td></tr>
  9. <tr><td>Africa</td><td>Burundi</td></tr>
  10. <tr><td>Africa</td><td>Cameroon</td></tr>
  11. <tr><td>Africa</td><td>Cape Verde</td></tr>
  12. </table>

更新:要添加lat和long,可以点击map()中的索引

  1. let globe = countries.map((country, index) => { // using map, we can transform each incoming country into a country/continent object
  2. let continent = 'N/A'; // just so we have somethign in case we dont find a continent
  3. [...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 [...]
  4. if (row.cells[1].innerText.trim() === country) continent = row.cells[0].innerText.trim();
  5. })
  6. return {
  7. country: country,
  8. continent: continent,
  9. lat: countryLat[index],
  10. long: countryLong[index]
  11. }; // we return an object back to our map
  12. });
展开查看全部

相关问题