Home > Net >  Replace array elements based on dictionary in javascript
Replace array elements based on dictionary in javascript

Time:10-09

I have a dictionary like this :

var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'}

I have an array like this :

var isoCodes = ['AFG','AFG','AFG','AGO,'AGO','AFG','AFG','AND','AGO']

I want to replace the above array to give me an output like this:

var isoCodes - ['AF','AF','AF','AO,'AO','AF','AF','AD','AO']

CodePudding user response:

Just use map

var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'};

var isoCodes = ['AFG','AFG','AFG','AGO','AGO','AFG','AFG','AND','AGO']

var result = isoCodes.map(x => iso_map[x]);

console.log(result);

  • Related