Home > Software design >  How to search for a string within an array substring
How to search for a string within an array substring

Time:12-22

I have an array like this.If atleast one of the substrings within the array have the value i'm searching for it should pass.

Value1 = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside
]

Using typescript i'm trying to make sure that atleast if a substring in the array there is the value Food. Even though the third substring within this arrat has no food it should still pass.

Code i've tried so far but does not do the job. It just returns the array

export function arraySubstring (expected:string, ignoreCase?: boolean): Expectation<string[], string> {
 return Expectation.that("test", expected, 
    async(actor: Actor, actual: string[]): Promise<boolean> ==> {

  try {
   for (const value of actual) {
     if(ignoreCase) {
       if (!value.toLowerCase().includes(expected.toLowerCase())) return 
         false;
     } else {
         if (!value.includes(expected)) return false;
     }
  }
  return true;
})
}

const value2= await webActor.attemptsTo(Ensure.that("my expectation", 
value1, arraySubstring ("Food")))

CodePudding user response:

Your code relies on it's order. If the first element that gets looped contains no "food", it will return false. Return statements always exit the function.

I'd suggest to create a single string and check the index of the first occurance with indexOf("food"), something like that.

const array = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside",
];

const found = array
  .reduce((obj, entry) => obj.concat(entry))
  .toLocaleLowerCase()
  .indexOf("food");
if (found >= 0) {
  console.log("Array contains food");
} else {
  console.log("Array does not contain food");
}

If you want to iterate you need to return on the true case, this will remove the importance of order from your code.

const array = [
  "Grape | 100 |Food Catering Service",
  "Apple | 100,000m |Food Catering Service",
  "Water Melon | 100,000m |Catering Service Outside",
  "100,000m |Food Catering Service Outside",
];

const ignoreCase = true;
const expected = "Food";

const containsExptected = (_array) => {
  for (const value of _array) {
    if (ignoreCase) {
      if (value.toLowerCase().indexOf(expected.toLowerCase()) >= 0) return true
    } else {
      if (value.indexOf(expected) >= 0) return true;
    }
  }
  return false;
};

const found = containsExptected(array);
if (found) {
  console.log("Array contains food");
} else {
  console.log("Array does not contain food");
}

CodePudding user response:

Here's a one-liner to check for a match of substring in an array of strings:

const value = ["Grape | 100 |Food Catering Service", "Apple | 100,000m |Food Catering Service", "Water Melon | 100,000m |Catering Service Outside", "100,000m |Food Catering Service Outside"]

const expected = "Food"
const ignoreCase = true
console.log(value.some(val => (ignoreCase ? val.toLowerCase().includes(expected.toLowerCase()) : val.includes(expected))))

  • Related