iterating through nested WinJS.Binding.List
-
Wednesday, August 22, 2012 3:32 PM
var transactions = Object.keys(currentItem.data.transactions._keyMap).map(function(key){return currentItem.data.transactions._keyMap[key].data}); for(var i=0;i< transactions.length; i++){ console.log(transactions[i].description + ": " + transactions[i].amount) }var transactionData1 = new WinJS.Binding.List([ { description: 'woolworths', amount: 536 }, { description: 'woolworths', amount: 1536 }, { description: 'woolworths', amount: 36 }, { description: 'woolworths', amount: 36 }, { description: 'woolworths', amount: 536 }, { description: 'woolworths', amount: 436 }, { description: 'woolworths', amount: 36 }, { description: 'woolworths', amount: 6 } ]); //Create sample data using a list var sampleData = new WinJS.Binding.List([ { name: 'sampleONE', category: "ATM & withdrawal", groupName: 'group2', spent: 345, ThreeMonthAvg: 456, spendTarget: 23, transactions: transactionData1 }, { name: 'sampleTWO', category: "ATM & withdrawal", groupName: 'group2', spent: 456, ThreeMonthAvg: 23, spendTarget: 345, transactions: transactionData1 }, { name: 'sampleTHREE', category: "ATM & withdrawal", groupName: 'group2', spent: 678, ThreeMonthAvg: 5, spendTarget: 456, transactions: transactionData1 }, { name: 'sampleFOUR', category: "ATM & withdrawal", groupName: 'group2', spent: 678, ThreeMonthAvg: 756, spendTarget: 23, transactions: transactionData1 }, { name: 'sampleFIVE', category: "ATM & withdrawal", groupName: 'group1', spent: 345, ThreeMonthAvg: 67, spendTarget: 56456, transactions: transactionData1 }, { name: 'sampleTHREE', category: "ATM & withdrawal", groupName: 'group1', spent: 234, ThreeMonthAvg: 342, spendTarget: 243, transactions: transactionData1 }, { name: 'sampleFOUR', category: "ATM & withdrawal", groupName: 'group1', spent: 1223, ThreeMonthAvg: 67, spendTarget: 3, transactions: transactionData1 }, { name: 'sampleFIVE', category: "ATM & withdrawal", groupName: 'group1', spent: 1323, ThreeMonthAvg: 345, spendTarget: 23, transactions: transactionData1 } ]);Above is an example of how I iterated through a nested list inside of a listView template... It works I don't feel that it's best practice, is there a better way?
All Replies
-
Friday, August 24, 2012 8:39 AMthanks. but how does this help me to iterate through a list using code?
-
Monday, August 27, 2012 11:57 AMModerator
HI
You can use foreach:
http://msdn.microsoft.com/en-us/library/windows/apps/hh700747(v=win.10).aspx
Then in your project use value.propertyName instead of value.
Hope it helpful.
- Proposed As Answer by Dino HeModerator Monday, August 27, 2012 11:57 AM
- Marked As Answer by Syllogism Monday, August 27, 2012 12:16 PM
- Unmarked As Answer by Syllogism Monday, August 27, 2012 12:23 PM
-
Monday, August 27, 2012 12:29 PM
Thanks, but the compiler throws an error because it doesn't recognize currentItem.data as a winJS.Binding.List (Object doesn't' support property or method 'forEach') Even though it is a list.
Is there any way that I can cast the currentItem.data object as a List object?
-
Wednesday, August 29, 2012 3:06 PMModerator
Hi,
Base on my understanding, currentItem is the selected item, not the list. It is needed to call forEach on transactionData1. If currentItem.data.transactions is a WinJS.Binding.List object, you can also call forEach on it.
Best Regards,
Ming Xu.
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework- Marked As Answer by Syllogism Wednesday, August 29, 2012 3:33 PM
-
Wednesday, August 29, 2012 3:35 PM
thanks.... can't believe I missed that
this works:
currentItem.data.transactions.forEach( function (value , index, array) { console.log('value: ' + value + ' index: ' + index + ' array: ' + array); } );


