Hello! I want to build this function for a school project. But I got stuck and I don’t know how to do. Please, give me some feedback.
explanation:
filterNgos(arrOfObj,"Health care") should return an array of objects which have the given string inside their value. If an object in arrOfObj has a key: health care donuts; and another object with a key: health care; those 2 objects should be pushed inside an array and that array is what the filterNgos returns.
const a = {
city: "Roma",
details: {responsiblePerson: "aldo", website: "www.teng.com", email: "teng@admin.com", facilityName: "Teng clinic"},
image: "gs://no-barriers.appspot.com/www.freepik.com-www.flaticon.com.png",
name: "Teng",
rating: 4,
service: "Health Care"};
const b = {
city: "milano",
details: {responsiblePerson: "marco", website: "www.teng.com", email: "teng@admin.com", facilityName: "Teng clinic"},
image: "gs://no-barriers.appspot.com/www.freepik.com-www.flaticon.com.png",
name: "Teng",
rating: 4,
service: "education"};
const c = {
city: "milano",
details: {responsiblePerson: "abc", website: "www.teng.com", email: "teng@admin.com", facilityName: "Teng clinic"},
image: "gs://no-barriers.appspot.com/www.freepik.com-www.flaticon.com.png",
name: "Teng",
rating: 1,
service: "school Care Health abc"};
const arrOfObj=[a, b, c];
const searchInNgo = (found, ngo, searchedWords) => {
const searchedWordsArray = searchedWords.split(" ");
// console.log("searchedWord", searchedWordsArray )
const searchedValue =
searchedWordsArray > 0
? new RegExp(searchedWordsArray.join("|"), "i")
: new RegExp(searchedWordsArray[0], "i");
console.log("searchedValue", searchedValue )
Object.keys(ngo).forEach((parameter) => {
if (typeof ngo[parameter] === "string" || "number"){
const isSearchedWordsInNgo =searchedValue.test(ngo[parameter]);
// console.log("ngo[key] to lowercase?", ngo[parameter].toLowerCase() )
// console.log("regEx", isSearchedWordsInNgo )
if (isSearchedWordsInNgo) {
found.push(ngo);
}
}
});
return found;
};
const filterNgos = (ngos, searchedWords) => ngos.filter(ngo =>
{const found = [];
searchInNgo(found, ngo, searchedWords);
return found}
)
filterNgos(arrOfObj,"Health care")