javascript - 如何根据另一个数组中的值获取数组中值的索引?
问题描述
好的,所以有两个数组。另一个是二维的,如下所示:
const locations = [
['London', 60.205490, 24.655899],
['Rome', 60.294411, 25.040070],
['Berlin', 60.262800, 24.824730],
['New York', 60.1698557, 24.9383791],
['Paris', 60.629951, 24.858080],]
const cities = ['London', 'Paris', 'Berlin' ]
我如何比较两个数组以获得这样的结果:
const indexArr = [0, 4, 2]
解决方案
我认为您正在尝试获得以下结果:
const locations = [
['London', 60.205490, 24.655899],
['Rome', 60.294411, 25.040070],
['Berlin', 60.262800, 24.824730],
['New York', 60.1698557, 24.9383791],
['Paris', 60.629951, 24.858080],]
const cities = ['London', 'Paris', 'Berlin' ]
let temp = [];
cities.forEach(function(someItem, someIndex){
locations.forEach(function(item, index){
if(item.indexOf(someItem) >= 0) {
temp.push(index);
}
})
})
console.log(temp);
推荐阅读
- android - 在 Flutter 中使用 Webview
- javascript - 如何从第三方 REST API 获取数据并显示在页面上
- r - 在 ifelse 条件和函数下创建多个列,其中参数取决于输出列的名称
- python - Django - 从视图到 HTML 的数据
- php - 使用 PHP 和 AJAX 选择的选项
- microsoft-teams - 在 Microsoft 团队桌面应用程序中注销 iframe
- python - 如何解释这个列表理解?
- python - 使用 itertools 循环计算行数
- google-apps-script - 将 Google 工作表导出为 .XLXS 并重命名
- r - Dplyr - 错误:由于类型不兼容(data.frame / data.frame),无法加入“Texto”x“Texto”