Here's the javascript code I'm working on. I am looping through a JSON file and trying to access the 'bought_together' values nested under 'related', but my current code doesn't seem to be doing what I want it to do. To debug, I'm using `document.body.innerHTML = 'hello';` to check if my if statement is functioning correctly. If this explanation is unclear, please let me know.
$(function() {
$.getJSON('test.json', function(data) {
$.each(data.products, function(key, val) {
for (var i = 0; i < data.products.length; i++) {
var obj = data.products[i];
if (obj.related == 'bought_together') {
document.body.innerHTML = 'hello';
}
}
});
})
})
Below is a snippet of the JSON file I'm referencing.
{
"products": [{
"asin": "B0001MQ60A",
"title": "KEEN Men's Newport H2 Sandal",
"imUrl": "http://ecx.images-
amazon.com / images / I / 41 pRaO7fFSL._SX395_.jpg ",
"related": {
"also_bought": ["B000MN8OR6"],
"also_viewed": ["B0000DYKET", "B0035FE60M", "B008KI85R4", "B000MQWVA4",
"B003Z4KHXS", "B00GUBOCGQ",
"B003O2SLXY", "B0017KSRMA", "B003O2SBKM", "B00DSN637U", "B000HDJ8IK", "B00E0J3HVG", "B003Z4KGZW",
"B005HO2CYG", "B003H4QFVY", "B0017LB2VC", "B002R8JPTK", "B008KI84SE", "B005DJDL9A", "B003TU14OE",
"B00E0J3HTI", "B000EDTVYY", "B003Z4JOJG", "B00DSN638E", "B00E0J3HVQ", "B008KI88JY", "B00EZIRE20",
"B0095RGEH2", "B00CEX6MSU", "B000B84URK", "B003O2SPMG", "B002KKCWP4", "B003O2SLXE", "B00JQHFV0M",
"B008JE8V14", "B0055ATVDW", "B003Z4KLMA", "B008ZAY40Y", "B003H4QFV4", "B00DSN64BU", "B002KKCZLA",
"B0055ATVV4", "B00HFY47JY", "B00DPHJUTW", "B008FWRJ6I", "B003Z4JUFO", "B00JFB4RL8", "B00HR1LTNM",
"B005HMTPBG", "B00KCT84I4", "B00HXDITEG"
],
"bought_together": ["B003O2SLXY", "B003H4QFVY", "B002R8JPTK", "B000EDTVYY"]
},
"salesRank": {
"Shoes": 18
},
"categories": [
["Clothing, Shoes & Jewelry", "Shoes & Accessories: International Shipping Available"],
["Clothing, Shoes & Jewelry", "K", "Keen"],
["Clothing, Shoes & Jewelry", "Comfort Shoes"],
["Clothing, Shoes & Jewelry", "Men", "Shoes", "Sandals"],
["Clothing, Shoes & Jewelry", "Men", "Shoes", "Athletic", "Sport Sandals"]
]
}]
}