The only examples I have been able to find of people using $.each are html samples, and it\'s not what I want. I have the following object:
var
Object properties do not have a defined order. They cannot be sorted. Arrays do have order. If you want the keys in a specific order, you will have to put them in an array and define the order.
You could grab all the property names (e.g. the keys) from the object, sort them and then iterate the properties in that order if you want. To do that, you'd do it like this:
var obj = {
obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
};
var keys = [];
for (var prop in obj) {
keys.push(prop);
}
keys.sort();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = obj[key];
// do whatever you want to do with key and value
}
As you will see, this requires an extra iteration to obtain and sort the list of keys. I'm not aware of any way around that. Obtaining the keys can be done in a modern browser with obj.keys(), but internally that's probably an iteration through the object properties anyway and you'd need a shim to allow that to work in older browsers.