简体   繁体   中英

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. 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??

You can use indexOf function to find the index of the county and then use it to find the code in the second array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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