I want to convert hash to array by javascript function in one line
a is hash which have value are
Object { 10="aa", 11="bb"}
and i want to convert it into
a=[10,"aa",11,"bb"]
Is there any methods which can convert it into array
Like this?
var obj = { 10: "aa", 11: "bb"}; var array = []; for( i in obj ) { array.push(i); array.push(obj[i]); }
var ob={10:"aa", 11:"bb"}; a = [];
one line
for(o in ob) a.push(Number(o), ob[o]);
For instance:
var obj = { 10: 'aa', 11: 'bb' };
to translate that into the Array you want, we can go like
var array = Object.keys( obj ).map(function( name ) { return [ +name ? +name : name, obj[ name ] ]; }).reduce(function( a, b ) { return a.concat(b); });