0

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?

3
  • 1
    It's an object, it doesn't have sorting on it's properties Commented Oct 27, 2016 at 13:41
  • 1
    Yes, has nothing to do with Angular this is Javascript. This may be of help. Commented Oct 27, 2016 at 13:44
  • Consider accepting answer if it is of any help Commented Nov 21, 2016 at 7:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.