How to get an array of objects given a keyword from another array of objects?

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")

please give more details on what the function should do and what instead is happening

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. I hope now it is clearer!

what logic are you using now? in plain english, explain the steps
of your code

I will start pointing out also some logic/syntax issues:

this always execute because "number" is truthy

|| is used with condition1 || condition2 and the two conditions are independent

I loop inside the main array, and then I loop inside of each object of the array, and I check if any of the string or number values includes the word or number I want. If the object has that value than I want to store it. In the end, I want to have a store with objects which have that word inside.

Right, but when you are checking the objects, are you checking a specific field?

Just some basic items.
The filter will return an array of valid ngos, so you don’t need to use push anywhere. Your searchInNgo should just return true/false to indicate to filter that the ngo should be in the final results.

const filterNgos = (ngos, searchedWords) => ngos.filter(ngo =>
searchInNgo(ngo, searchedWords))

Rather than forEach on your keys array, consider Array.some, since your result is if some parameter matches the search words then keep the ngo.

1 Like

yes, I check if one of the key-value pair has a specific word inside its string value

Thank you a lot! I solved!!

I know you’ve solved it, but that didn’t really answer the question. I know you’re checking to see if the value of a property contains a given value. What I was asking is is that value going to be for a specific property.

It looks as if you’re checking every property
and looping despite knowing what the object properties are up front. If you say “word”, then you are looking for a specific string, and there are only a few properties that need be checked (and only services that actually makes sense given the example you show at the end)

1m

I didn’t understand it well, sorry for that. The property is not specific but the value should be. I should get every object which contains the value I am searching which can be a service type, or the name of the organization or the city or rating. For example if I search for milan, it should give me every object which has a word milan somewhere inside the object. Or if I search for milan 4 it should give me all objects which have a word milan or the number/string 4 inside it. The only property I don’t check are details and image. You can try here what I mean. https://repl.it/@FadimeOzdemir/searchInNGO#index.js Sorry again for my bad English and inability to explain. I don’t know how to explain it properly. To me it seems specific value.

1 Like

No,no that’s great, your English is fine, I just wanted to figure out if there was a simpler way to do it