Fixed single object - Bug:#4

This commit is contained in:
agusmakmun
2017-01-18 14:54:52 +07:00
parent 88c6438090
commit e0158c5fff

View File

@@ -110,11 +110,18 @@
}
searchResultsEl.style.offsetWidth;
var matchingPosts = posts.filter(function (post) {
if ((post.title + '').toLowerCase().indexOf(currentInputValue) !== -1 || (post.description + '').toLowerCase().indexOf(currentInputValue) !== -1) {
return true;
}
});
var matchingPosts;
// check the `posts` object is single or many objects.
// if posts.title === undefined, so posts is many objects.
if(posts.title === undefined) {
matchingPosts = posts.filter(function (post) {
if ((post.title + '').toLowerCase().indexOf(currentInputValue) !== -1 || (post.description + '').toLowerCase().indexOf(currentInputValue) !== -1) {
return true;
}
});
}else {
matchingPosts = [posts]; // assign single object to Array
}
if (!matchingPosts.length) {
searchResultsEl.classList.add('is-hidden');
}