I'd like to convert a search result received from server to unordered list:
function searchSuccess(json, textStatus, jqXHR) {
console.log('json is:', json);
var html= "<ul>";
Object.keys(json).forEach(function(key, val) {
html += "<li><a href=\'" + val['slug'] + "\'>" + val['title'] +"</a></li>";
});
html +="</ul>"
$('#search-results').append(html);
}
The json, as I see in the console is like:
json is: [{"title":"Hello World","slug":"hello-world"},{"title":"I'm a title","slug":"I-am-title"}]
However, instead of the linked li , a list of undefined is rendered.
What is wrong here? How can I fix it?
console.log(key, val)in yourforEachand take a look at what they actually are. (valhere is going to be an index)jsonis already parsed or if it is still in string format.forEachisfunction(element, index), trying to doval[key]is a quick issue to spot