I would like to select a particular value using jquery each from the second and third level array. This is my JSON response looks like:
{
"events": [{
"id": 847843,
"name": "Strasbourg IG at Limoges CSP",
"timezone": "GMT",
"timezoneOffset": 0,
"time": 1401983400000,
"timeUtc": "2014-06-05T18:50:00+00:00",
"status": "live",
"wpRef": "847843",
"parentCategoryId": 22,
"parentCategoryName": "Basketball",
"categoryId": 7443,
"categoryName": "French Championnat Pro A ",
"racingEvent": null,
"raceNumber": null,
"marketCount": 3,
"prefix": null,
"nameSeparator": null,
"markets": [{
"id": 4943131,
"primitiveType": 2,
"primitiveName": "Head to Head",
"typeId": 36,
"name": "Head to Head",
"marketDescription": "Select the winner of the match.",
"marketRules": "Includes extra time.\r",
"eventNotices": null,
"autoHide": false,
"status": "live",
"bettingStatus": "enabled",
"period": "Fulltime",
"allowMultiple": true,
"betInRunTime": 0,
"track": null,
"weather": null,
"betTypes": [{
"id": 1,
"name": "Win",
"finishingOrder": null,
"products": []
}],
"selections": [{
"id": 288481048,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Strasbourg IG",
"prefix": "",
"mpId": 288481048,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 2.6,
"usPrice": "+160",
"value": 2.60
}]
}, {
"id": 288481039,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Limoges CSP",
"prefix": "",
"mpId": 288481039,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 1.44,
"usPrice": "-227",
"value": 1.44
}]
}]
}, {
"id": 4943134,
"primitiveType": 99,
"primitiveName": "Other",
"typeId": 1266,
"name": "Odd or Even Total",
"marketDescription": "Select whether the amount of points scored in the match will be an odd or even number (zero = even)\r\rIncludes overtime.",
"marketRules": "For this market zero will be deemed to be an even number.\r\rNo bonus bets.\r\rIncludes overtime.",
"eventNotices": null,
"autoHide": true,
"status": "live",
"bettingStatus": "enabled",
"period": "Fulltime",
"allowMultiple": true,
"betInRunTime": 0,
"track": null,
"weather": null,
"betTypes": [{
"id": 1,
"name": "Win",
"finishingOrder": null,
"products": []
}],
"selections": [{
"id": 288481066,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Even",
"prefix": "",
"mpId": 288481066,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 1.85,
"usPrice": "-118",
"value": 1.85
}]
}, {
"id": 288481075,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Odd",
"prefix": "",
"mpId": 288481075,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 1.85,
"usPrice": "-118",
"value": 1.85
}]
}]
}, {
"id": 4943137,
"primitiveType": 99,
"primitiveName": "Other",
"typeId": 1298,
"name": "Match Result",
"marketDescription": null,
"marketRules": null,
"eventNotices": null,
"autoHide": false,
"status": "live",
"bettingStatus": "enabled",
"period": "Fulltime",
"allowMultiple": true,
"betInRunTime": 0,
"track": null,
"weather": null,
"betTypes": [{
"id": 1,
"name": "Win",
"finishingOrder": null,
"products": []
}],
"selections": [{
"id": 288481102,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Strasbourg IG",
"prefix": "",
"mpId": 288481102,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 2.63,
"usPrice": "+163",
"value": 2.63
}]
}, {
"id": 288481084,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Limoges CSP",
"prefix": "",
"mpId": 288481084,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 1.5,
"usPrice": "-200",
"value": 1.50
}]
}, {
"id": 288481093,
"betId": null,
"eventId": null,
"eventNumber": null,
"eventName": null,
"eventTime": null,
"position": null,
"name": "Draw",
"prefix": "",
"mpId": 288481093,
"marketPosition": null,
"marketName": null,
"marketTypeName": null,
"marketSelectionId": null,
"marketSelectionName": null,
"categoryId": null,
"categoryName": null,
"parentCategoryId": null,
"parentCategoryName": null,
"maxBet": 0.00,
"scratched": false,
"winPrice": null,
"placePrice": null,
"finalWinPrice": null,
"finalPlacePrice": null,
"finalPrice": null,
"prices": [{
"betTypeId": 1,
"betTypeName": "Win",
"productId": null,
"productName": null,
"precisePrice": 13.00,
"usPrice": "+1200",
"value": 13.00
}]
}]
}],
"result": null
}]
}
I am able to fetch all the "name" field from events category using the below code.
drawAllEvents: function (events) {
var $container = $('#someID ul');
$.each(events, function(i, event) {
$('<a />', { text: event.name, href: '#' }).wrap('<li />').parent().appendTo($container);
});
}
Now I am trying to fetch "primitiveName" field from the markets array using another function but for some reason it returns undefined. Here is my code
drawAllBetTypesFilter: function (events) {
$('#bettype-sport ul').empty();
var betTypeFilter = $('#bettype-sport ul');
$.each(events, function (i, event) {
$.each(event.markets, function (market) {
var betTypeName = market.primitiveName;
$('<a />', {
text: betTypeName
}).wrap('<li />').parent().appendTo(betTypeFilter);
});
});
}
Furthermore, How do I fetch third level array, for e.g If I want to fetch all "name" fields under "betTypes" which is a child of "markets" object.
EDIT: 1 - Also mentioned in my comment. How do I group similar primitiveName values together in my list instead of displaying them seperately.
Here is the fiddle to show how am I fetching values in first level. FIDDLE LINK
The first parameter passed to the each() iteration function is the index of the current element in the array, so in your current code your market parameter is not the element from the array but instead an int. Try this:
var betTypeFilter = $('#bettype-sport ul');
$.each(events, function (i, event) {
$.each(event.markets, function (i, market) { // <- note 'i' first param
var betTypeName = market.primitiveName;
$('<a />', {
text: betTypeName
}).wrap('<li />').parent().appendTo(betTypeFilter);
// to get betTypes for this market:
$.each(market.betTypes, function(i, betType) {
// do something...
console.log(betType);
});
});
});
To fetch the name under betTypes would take another loop. Or, if there is only ever one object in the array you could hard-code access to the first element only:
market.betTypes[0].name; // == 'Win'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With