Reputation: 2422
I'm new to Angular and having trouble trying to parse a JSON response from this API: http://icd10api.com/?s=C50&desc=long&r=json
in this codepen here: http://codepen.io/FritzAPI/pen/dNZWKZ
which I got working with a hardcoded modified response:
function loadVegetables() {
var veggies = [
{"Name":"C50","Description":"Malignant neoplasm of breast"},{"Name":"C50.0","Description":"Malignant neoplasm of nipple and areola"},{"Name":"C50.01","Description":"Malignant neoplasm of nipple and areola, female"},{"Name":"C50.011","Description":"Malignant neoplasm of nipple and areola, right female breast"},{"Name":"C50.012","Description":"Malignant neoplasm of nipple and areola, left female breast"},{"Name":"C50.019","Description":"Malignant neoplasm of nipple and areola, unspecified female breast"},{"Name":"C50.02","Description":"Malignant neoplasm of nipple and areola, male"},{"Name":"C50.021","Description":"Malignant neoplasm of nipple and areola, right male breast"},{"Name":"C50.022","Description":"Malignant neoplasm of nipple and areola, left male breast"},{"Name":"C50.029","Description":"Malignant neoplasm of nipple and areola, unspecified male breast"}
];
return veggies.map(function (veg) {
veg._lowername = veg.Name.toLowerCase();
veg._lowertype = veg.Description.toLowerCase();
return veg;
});
}
but as soon as the response is wrapped in a search array {"Search":[...]} this function no longer works.
Upvotes: 0
Views: 424
Reputation: 27192
DEMO
var jsonObj = {"Search":[{"Name":"C50","Description":"Malignant neoplasm of breast"},{"Name":"C50.0","Description":"Malignant neoplasm of nipple and areola"},{"Name":"C50.01","Description":"Malignant neoplasm of nipple and areola, female"},{"Name":"C50.011","Description":"Malignant neoplasm of nipple and areola, right female breast"},{"Name":"C50.012","Description":"Malignant neoplasm of nipple and areola, left female breast"},{"Name":"C50.019","Description":"Malignant neoplasm of nipple and areola, unspecified female breast"},{"Name":"C50.02","Description":"Malignant neoplasm of nipple and areola, male"},{"Name":"C50.021","Description":"Malignant neoplasm of nipple and areola, right male breast"},{"Name":"C50.022","Description":"Malignant neoplasm of nipple and areola, left male breast"},{"Name":"C50.029","Description":"Malignant neoplasm of nipple and areola, unspecified male breast"}],"totalResults":"82","Response":"True"};
var res = jsonObj.Search.map(item => {
item._lowername = item.Name.toLowerCase();
item._lowertype = item.Description.toLowerCase();
return item;
});
console.log(res);
Upvotes: 0
Reputation: 3128
It seems like your hardcoded object is just slightly different than the response from the API. The API returns an object with a Key Search
. The value of Search
is what your veggies
array is imitating. So something like:
apiResponse.Search.map(function (veg) {
veg._lowername = veg.Name.toLowerCase();
veg._lowertype = veg.Description.toLowerCase();
return veg;
});
Upvotes: 1