Actually, I may have spoken too soon - I have an issue now where for some reason the values I see in the console assigned to the array are populating the variables incorrectly - but not always! Updated code: function sortObject(obj) {
var arr = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
arr.push({
'key': prop,
'value': obj[prop]
});
}
}
arr.sort(function(a, b) { return b.value - a.value; });
//arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings
return arr; // returns array
}
var list = {"1000": utag_data["va.metric_sets.5155.Riding Apparel & Clothing"], "2000": utag_data["va.metric_sets.5155.Riding Boots and Chaps"], "3000": utag_data["va.metric_sets.5155.Horse Tack"], "4000": utag_data["va.metric_sets.5155.Horse Blankets"], "5000": utag_data["va.metric_sets.5155.Horse Care"], "6000": utag_data["va.metric_sets.5155.Stable Supplies"], "7000": utag_data["va.metric_sets.5155.Gifts"], "8000": utag_data["va.metric_sets.5155.CLOSEOUTS"], "9000": utag_data["va.metric_sets.5155.Sale"]};
console.log(list);
var arr = sortObject(list);
console.log(arr);
b.FavCat1 = arr[0].key;
b.FavCat2 = arr[1].key;
b.FavCat3 = arr[2].key;
b.FavCat4 = arr[3].key; I dump the list to the console to check it- looks good: Object {1000: 51, 2000: 19, 3000: 41, 4000: 10, 5000: 78, 6000: 10, 7000: 2, 8000: 7, 9000: 13} I dump the array to the console to check it - looks good: [Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
key: "5000"
value: 78
__proto__: Object
1: Object
key: "1000"
value: 51
__proto__: Object
2: Object
key: "3000"
value: 41
__proto__: Object
3: Object
key: "2000"
value: 19
__proto__: Object
4: Object
5: Object
6: Object
7: Object
8: Object
length: 9 Then I check what got assigned in utag_data to FavCat1, 2, 3, 4 - utag_data
Object {site_region: "usa", site_currency: "usd", page_name: "Home", page_type: "Home", pageType: "other"…}
FavCat1: "1000"
FavCat2: "1000"
FavCat3: "1000"
FavCat4: "1000"
_cbrand: Array[0]
_ccat: Array[0]
_ccat2: Array[0]
_ccity: "".... Any ideas? Thanks! Monique
... View more