简体   繁体   中英

how to keep the order of keys when when an object pushed to an array

I have an object as below

var obj = { el: 'el', app: 'app'};
var arr = [];
// pushed obj to arr array
arr.push(obj);

When I looked at the console, arr[0] has the obj with sorted keys such as {app: 'app', el:'el'} .

My requirement here is to keep the order same.

How can I keep the order same?

You can try

var obj = { el: 'el', app: 'app' };
            var arr = [];
            jQuery.each( obj, function ( i, val )
            {
                arr.push( obj[i] );
            } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM