I’ve got a list with ~1000 possible file extensions and it’s aliases. The structure looks like this:
var iconMap = [
{ icon: 'photoshop', extensions: ['psd'], format: 'svg' },
{ icon: 'photoshop2', extensions: ['psd'], format: 'svg', disabled: true },
{ icon: 'php', extensions: ['php1', 'php2', 'php3', 'php4', 'php5', 'php6', 'phps', 'phpsa', 'phpt', 'phtml', 'phar'], format: 'svg' }]
I want to get the ‘icon’ property for the according extension. Therefore I want to create a getExtensionIcon(fileExtension)
function which should return:
var phpIcon = getExtensionIcon('php') // 'php'
var pharIcon = getExtensionIcon('phar') // 'php'
var php5Icon = getExtensionIcon('php5') // 'php'
My question:
I only got a few ideas which would be very ineffecient, so I am curious how you would realize the getExtensionicon function?
2
Answers
If you can use jQuery, it has the
$.grep()
function which seems like a good fit for this purpose:I don’t really know how performant this is, but you should at least give it a try 😉
Testing with your Example:
Here is how I would do it – this “pre-processes” the iconMap once, creating an object that keys
extension
with a value oficon