I moved the vars up (there's no block scope in js), and separated the cas

I moved the vars up (there's no block scope in js), and separated the case for object map vs array. Changed replace/regex for split/join.