简体   繁体   中英

Angular default sorting in object

I created some object and put key, value into him.

var obj = {};

obj.Z = "val1";
obj.Y = "val2";
obj.X = "val3";

but keys sorting in object by default. In result order next:

X:"val3"
Y:"val2"
Z:"val1"

How does prevent this sorting, that sorting was as had put to original obj?

In pure javascript you can do like this

 var obj={}; obj.Z = "val1"; obj.Y = "val2"; obj.X = "val3"; var newObj={} Object.keys(obj) .sort() .forEach(function(key, value) { newObj[key]=obj[key]; }); console.log(newObj); 

Hope this helps

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