简体   繁体   English

如何找到一个数组的索引并将该索引值用于另一个数组中的 select 值

[英]How to find the index of one array and using that index value to select value from another array

const COUNTY = ["DELHI","GUJARAT","GOA","ANDHRA"]

const COUNTY_CODE = ["001","002","003","004"]

We have 2 arrays. First array, we are showing in dropdown.我们有 2 个 arrays。第一个数组,我们在下拉列表中显示。 We need to pass the county code from 2nd array based on the county selected in 1st array.我们需要根据第一个数组中选择的县从第二个数组传递县代码。

How to do it from using angular JS and javascript and typescript??如何使用 angular JS 和 javascript 和 typescript 来做到这一点?

You can use indexOf function to find the index of the county and then use it to find the code in the second array.您可以使用indexOf function 来查找县的索引,然后使用它来查找第二个数组中的代码。

 const COUNTY = ["DELHI","GUJARAT","GOA","ANDHRA"] const COUNTY_CODE = ["001","002","003","004"] const selected = "DELHI"; const selectedIndex = COUNTY.indexOf(selected); const code = COUNTY_CODE[selectedIndex]; console.log(code);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM