英文:
Search for a string in an array of strings using AND condition
问题
如何使 includes()
在 some()
中匹配所有情况?
let instance = ['m6gd.medium', '1 vCPU', '4 GiB'];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - 需要使这个返回 false - 需要 AND 条件
let found = instance.some(s1 =>
{
console.log("s1 = ", s1.toLowerCase());
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
console.log("search_term = ", search_term);
return search_term.split(' ').some(s2 =>
{
console.log("s2 = ", s2.toLowerCase(), "\n");
return s1.toLowerCase().includes(s2.toLowerCase())
});
});
console.log(found);
英文:
How do I make includes()
in some()
match all cases ?
let instance = ['m6gd.medium', '1 vCPU', '4 GiB'];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - need to make this false - need AND condition
let found = instance.some(s1 =>
{
console.log("s1 = ", s1.toLowerCase());
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
console.log("search_term = ", search_term);
return search_term.split(' ').some(s2 =>
{
console.log("s2 = ", s2.toLowerCase(), "\n");
return s1.toLowerCase().includes(s2.toLowerCase())
});
});
console.log(found);
答案1
得分: 2
以下是翻译的代码部分:
使用`array.foreach`在拆分搜索条件上,并在`array.foreach`的回调中测试对`haystack`上的某个函数的调用是否返回`true`。以下是我将如何执行此操作的方式。
```javascript
let instance = ["m6gd.medium", "1 vCPU", "4 GiB"];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - 需要将其设置为false - 需要AND条件
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
let notFound = false;
// 现在为false,但我们将拆分搜索条件,
// 然后对每个拆分项目执行array.foreach以查看它是否包含在instance数组中。如果是,这将变为true
search_term.split(" ").forEach(el => {
if (!instance.some(s => {
return s.toLowerCase().includes(el.toLowerCase())
})) {
notFound = true;
}
})
console.log(!notFound);
谢谢您的问题,实际上似乎没有必要使用forEach
,而且可能运行得更快。我经验不足,但以下是代码:
let instance = ["m6gd.medium", "1 vCPU", "4 GiB"];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - 需要将其设置为false - 需要AND条件
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
let result = compare(search_term.split(" "), instance)
function compare(searchArray, comparisonArray) {
// 将比较数组中的所有项转换为小写
const comparisonArrayLowercase = comparisonArray.map(item => item.toLowerCase());
// 检查搜索数组中的每个项是否是至少一个比较数组中项的子字符串
return searchArray.every(searchItem =>
comparisonArrayLowercase.some(
comparisonItem => comparisonItem.includes(searchItem.toLowerCase())
)
);
}
console.log(result);
这是您提供的代码的翻译部分。如果您需要更多的信息或有其他问题,请随时提问。
英文:
Use array.foreach on the split search term, and in callback for array.foreach test that calling some function on the haystack returns true. here is how I would do it.
let instance = ["m6gd.medium", "1 vCPU", "4 GiB"];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - need to make this false - need AND condition
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
let notFound = false;
//false for now, but we will split search term,
//then perform array.foreach on each split item to see if it is contained in instance array. If so, this becomes true
search_term.split(" ").forEach(el => {
if(!instance.some(s => {
return s.toLowerCase().includes(el.toLowerCase())
}))
{
notFound = true;
}
})
console.log(!notFound);
Thanks for the question, indeed it appears there is a way without foreach, it may run faster as well. I am not experienced enough to know, but here is the code:
let instance = ["m6gd.medium", "1 vCPU", "4 GiB"];
// let search = "medium"; // true
// let search = "4gb"; // true
// let search = "medium 4gb"; // true
let search = "medium 12gb"; // true - need to make this false - need AND condition
let search_term = search.toLowerCase();
search_term = search_term.replace(/gb/g, "gib");
search_term = search_term.replace(/(\d+)(gib)/g, "$1 $2");
let result = compare(search_term.split(" "), instance)
function compare(searchArray, comparisonArray) {
// Convert all items in the comparison array to lowercase
const comparisonArrayLowercase = comparisonArray.map((item) => item.toLowerCase());
// Check if every item in the search array is a substring of at least one item in the comparison array
return searchArray.every((searchItem) =>
comparisonArrayLowercase.some(
(comparisonItem) => comparisonItem.includes(searchItem.toLowerCase())
)
);
}
console.log(result);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论