I have to iterate through "price_detail" json array object. Please teach me how to reinerate this type of object. I have looped through many structures but not this type. I could not find an example for this type of data structure searching for "Iterate through nested json object array"
Code I use:
$.each(json.data['price_detail'], function (i, item) {
console.log('name='+ i + ' value=' +item);
}
output:
name=price value=14.7,14.7,14.7
name=type value=coupon,coupon,coupon
name=savings value=,75%,35%
name=pharmacy value=Walmart,Kmart,Costco
Data has following structure.
{"errors": [],
"data": {"form": "tablet",
"price_detail":
{"price": [14.7, 14.7, 14.7],
"type": ["coupon", "coupon", "coupon"],
"savings": [null, "75%", "35%"],
"pharmacy": ["Walmart", "Kmart", "Costco"]},
"brand": ["lortab", "maxidone", "vicodin", "norco", "xodol", "hycet"],
"dosage": "5mg/325mg", "generic": ["hydrocodone/acetaminophen", "lorcet", "zolvit"],
"prices": [14.7, 14.7, 14.7],
"quantity": 60,
"display": "Lortab, Maxidone, Vicodin, Norco, Xodol, Hycet (hydrocodone / acetaminophen, lorcet, zolvit)",
"manufacturer": "generic"},
"success": true}
I would like an object using pharmacy, type, saving and prices; such as:
{
["Walmart", "coupon", NULL, 14.7],
["Kmart", "coupon", "75%", 14.7],
["Costco","coupon", "35%", 14.7]
}
Thank you.
EDIT: I changed the title. After scouring the web, I discovered I need to transpose the json result. Pardon my lack of terminology.
I attempted to use this function I found to transpose an array
function transpose(a) {
return Object.keys(a[0]).map(function (c) {
return a.map(function (r) {
return r[c];
});
});
}
created test data, run it through function
var testData = [ [14.7, 14.7, 14.7],
["coupon", "coupon", "coupon"],
[null, "75%", "35%"],
["Walmart", "Kmart", "Costco"]
]
var result = transpose(testData);
This gives me the desired results.
[14.7, "coupon", null, "Walmart"],
[14.7, "coupon", "75%", "Kmart"],
[14.7, "coupon", "35%", "Costco"]
Now when I run my 'price_detail' object through it
transpose(json.data['price_detail']);
I get stuck at this error: Uncaught TypeError: Cannot convert undefined or null to object