Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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]); }
Add a comment
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); });
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.