0

I have an object of objects. And I want to show the objects and its values in specific order.(not alphabetically)

How can I achieve that with angular? Here it is my sample object

var filters = {
    language : { someProperty : "prop1", someOther : "prop2" },
    country : { resumeProp : "prop", resumeProp2 : false },
    destination { resumeProp : "prop", resumeProp2 : false },
};

I want to arrange example destination, country and then language.

3
  • How are you going to be displaying the object? Commented May 11, 2016 at 11:39
  • I will display the keys and the iterate over their values with ng-repeat. But I want to display the keys in a particular order. Commented May 11, 2016 at 11:52
  • If you want your custom ordering scheme as in your example: destination, country and language, you will need to do that manually. But if you want a lexicographic sort, than you can sort the keys and get the values based on those keys. Commented May 11, 2016 at 12:11

1 Answer 1

1

JavaScript objects are unordered by definition (see the ECMAScript Language Specification, section 4.3.3). The language specification doesn't even guarantee that, if you iterate over the properties of an object twice in succession, they'll come out in the same order the second time.

If you need things to be ordered, use an array and the Array.prototype.sort method:

var filters = [
    { name: "language", order: 2, someProperty : "prop1", someOther : "prop2" },
    { name: "country", order: 1, resumeProp : "prop", resumeProp2 : false },
    { name: "destination", order: 0, resumeProp : "prop", resumeProp2 : false }
];

function compare(a,b) {
  if (a.order < b.order)
    return -1;
  else if (a.order > b.order)
    return 1;
  else 
    return 0;
}

filters.sort(compare); // destination, country, language

If you are coding in ES6 you can use the Map object which is similar to an Object and guarantees key order.

If the original object cannot be modified then

  1. create another array with the object's indexes like var filtersKey = ['destination', 'country', 'language'];

  2. run the ng-repeat on that array.

  3. get the values from the original object using filters[value] where value represents each string contained in filtersKey exposed by the ng-repeat.

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

3 Comments

I cannot add new objects in my array as you did 'order'. I want to sort the keys only and get the values accordingly
@BesaNeziri then you can define a new array like var filtersKey = ['destination', 'country', 'language'] and run the ng-repeat on that array. Then with the value you get filters[value] from the other array. Directly sorting object keys is impossible.
@BesaNeziri I updated the answer, please accept it :)

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.