loop through javascript object with customized order
I've been looking all over for a solution to my problem but can't seem to
find any answers...so I'm not sure it's possible.
I have a javascript object with products info, so here's what it sort of
looks like (note that it's already ordered the way I want it to be):
var prodType = {
'7' : {'prodname' : 'Abel','prodtype' : 'Book', 'prodprice' : '49.00'},
'212' : {'prodname' : 'Carl','prodtype' : 'Book', 'prodprice' : '49.00'},
'211' : {'prodname' : 'Devon','prodtype' : 'Book', 'prodprice' : '49.00'},
'2' : {'prodname' : 'Sarah','prodtype' : 'Book', 'prodprice' : '49.00'},
'10' : {'prodname' : 'Walter','prodtype' : 'Book', 'prodprice' : '49.00'}
}
And I loop through it to create a table for a certain plugin, so an
example of how I get the info:
for(var val in prodType) {
console.log('pid: '+val
+ ' pname: '+prodType[val].prodname
+ ' pcategory: '+prodType[val].prodtype
+ ' pprice: '+prodType[val].prodprice);
}
My problem is: I need to have it print ordered by prodname but keep the id
as is - just like I'm providing it, since I'm using the ID for further DB
handling. So, right now it prints:
pid: 2 pname: Sarah pcategory: Book pprice: 49.00
pid: 7 pname: Abel pcategory: Book pprice: 49.00
pid: 10 pname: Walter pcategory: Book pprice: 49.00
pid: 211 pname: Devon pcategory: Book pprice: 49.00
pid: 212 pname: Carl pcategory: Book pprice: 49.00
And I need it to look like this:
pid: 7 pname: Abel pcategory: Book pprice: 49.00
pid: 212 pname: Carl pcategory: Book pprice: 49.00
pid: 211 pname: Devon pcategory: Book pprice: 49.00
pid: 2 pname: Sarah pcategory: Book pprice: 49.00
pid: 10 pname: Walter pcategory: Book pprice: 49.00
Is is possible? If so, how? Thanks a bunch!
No comments:
Post a Comment