Skip to content

Instantly share code, notes, and snippets.

@milani
Created August 23, 2012 09:34
Show Gist options
  • Save milani/3434679 to your computer and use it in GitHub Desktop.
Save milani/3434679 to your computer and use it in GitHub Desktop.
Best way to convert an accept type object to equv. string
Best way to convert acceptType object to a string like below?
From:
var acceptTypes = {
'Images': ['*.png','*.jpg'],
'Documents': ['*.docx']
}
To:
var acceptType = 'Images:*.png,*.jpg;Documents:*.docx'
@polotek
Copy link

polotek commented Aug 23, 2012

var out = Object.keys(acceptTypes).reduce(function(acc, key) {
  var val = acceptTypes[key]
  acc.push(key + ':' + val.join(','));
  return acc;
}, []).join(';')

@ljharb
Copy link

ljharb commented Aug 23, 2012

@polotek lol i came up with the exact same solution (with the 2 semicolons you're missing, of course)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment