Please I need help on this code. I have created a page with a single input field with the name="searchWork" where I input the search keyword and send.
On the "if" statement where the boolean resides, only the first comparison works (the comparison against the .state element, but the .lgaName and the .nameOfCommunity are not working. That is whenever I search the states in my object array, the object get populated with relevant data, but when I searched lga or community name, it displays an empty object, meaning the object is not populated with any data. I don't know where I went wrong on the comparison or something.
Thank you.
function getSearch(req, res, next) {
let theProjects = shared.projects;
let dataObject = {};
const search = req.body.searchWork;
for (let i = 0; i < theProjects.length; i++) {
if ((search === theProjects[i].state) || (search === theProjects[i].lga[i].lgaName) || (search === theProjects[i].lga[i].communities[i].nameOfCommunity)) {
dataObject.state = theProjects[i].state;
dataObject.lga = theProjects[i].lga[i].lgaName;
dataObject.community = theProjects[i].lga[i].communities[i].nameOfCommunity;
dataObject.project = theProjects[i].lga[i].communities[i].projects.building;
}
}
console.log(dataObject);
next();
}