In the world of JavaScript, objects are inherently unordered (as stated in the ECMAScript Language Specification, section 4.3.3). It's not guaranteed that iterating over a set of object properties twice will yield the same order both times.
To ensure order, one should opt for an array and leverage 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're working with ES6, consider using the Map object; it operates like an Object but guarantees key order as well.
If altering the original object is not feasible,
generate another array featuring the object's indexes, such as
var filtersKey = ['destination', 'country', 'language'];
apply the ng-repeat to this new array.
fetch values from the original object via filters[value]
, where value
corresponds to each string within filtersKey accessed through the ng-repeat.