【问题标题】:如何在数组子字符串中搜索字符串
【发布时间】:2022-01-23 07:25:34
【问题描述】:

我有一个这样的数组。如果数组中的至少一个子字符串具有我正在搜索的值,它应该通过。

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
]

使用打字稿我试图确保至少如果数组中的子字符串存在值 Food。 即使此 arrat 中的第三个子字符串没有食物,它仍然应该通过。

到目前为止我已经尝试过但没有完成这项工作的代码。它只是返回数组

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

【问题讨论】:

  • edit您的问题向我们展示您需要的输出。你的书面规范不是很清楚。
  • @O.Jones 更具解释性。所以如果我声明 const value2.如果它比较数组 value1 和 Food。我的断言应该是正确的
  • 另外请记住正确缩进您的代码,因为您希望人们能够轻松阅读您的代码。
  • 为什么第三个没有关键字的要通过?
  • @charlietfl 如果数组中的至少一个子字符串具有我正在搜索的值,则它应该通过。既然至少有 3 个,那么它应该可以通过。

标签: javascript arrays typescript


【解决方案1】:

您的代码依赖于它的顺序。如果循环的第一个元素不包含“食物”,它将返回 false。 return 语句总是退出函数。

我建议创建一个字符串并使用indexOf("food") 检查第一次出现的索引,类似这样。

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

如果你想迭代你需要返回真实的情况,这将消除你代码中顺序的重要性。

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

【讨论】:

  • 不是&gt;=0而不是&gt;0吗?
  • 好的,谢谢。
  • 感谢@Deepak 我在我的代码中修复了这个问题
【解决方案2】:

这是一个检查字符串数组中子字符串匹配的单行代码:

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

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2023-04-05
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多