英文:
Is there any way to check if a given string exists in any array element?
问题
假设存在给定字符串,`s="abc"`
有没有办法检查该给定字符串是否存在于数组中,比如,
arr=["pnq","dwksnn","inabcpi","poals"]
有没有任何方法/方式可以在这种情况下返回true条件?
英文:
Suppose a given string exists, s="abc"
Is there any way to check if that given string exists in an array, such as,
arr=["pnq","dwksnn","inabcpi","poals"]
Any method/way to see so that a condition will return true in such a case?
答案1
得分: 3
你只需要迭代给定的数组,并检查这个字符串是否存在于数组中的每个字符串中。
const arr = ['pnq', 'dwksnn', 'inabcpi', 'poals'];
const stringToFind = 'abc';
const doesStringExist = arr.find(item => item.includes(stringToFind));
// 或者如果你不想知道给定字符串是哪个字符串的一部分,可以使用`Array.prototype.some`
const doesStringExist = arr.some(item => item.includes(stringToFind));
console.log(`The string ${stringToFind} ${doesStringExist ? 'Exists' : 'Doesn't Exist'}`);
英文:
Simple, you need to iterate over the given array and check if this string exists in each of the strings within the array.
const arr = ['pnq', 'dwksnn', 'inabcpi', 'poals'];
const stringToFind = 'abc';
const doesStringExist = arr.find(item => item.includes(stringToFind));
// OR if you don't want to know which string the given string is a part of, you could use `Array.prototype.some`
const doesStringExist = arr.some(item => item.includes(stringToFind));
console.log(`The string ${stringToFind} ${doesStringExist ? 'Exists' : 'Doesn\'t Exist'}`);
答案2
得分: 1
需要搜索每个数组元素并检查给定的字符串是否包含在任何元素中,您可以使用some()
方法。以下是JavaScript代码:
const s = "abc";
const arr = ["pnq", "dwksnn", "inabcpi", "poals"];
const is_exists = arr.some(element => element.includes(s));
console.log(is_exists); // 如果存在则为true,否则为false
英文:
you need to search over each array element and check if the given string is contained within any of the elements, you can use the some()
method. Here's the JavaScript code:
const s = "abc";
const arr = ["pnq", "dwksnn", "inabcpi", "poals"];
const is_exists = arr.some(element => element.includes(s));
console.log(is_exists); // true if exists, otherwise false
答案3
得分: 1
你可以使用数组方法 some
和字符串方法 includes
。
const arr = ['pnq', 'dwksnn', 'inabcpi', 'poals'];
// 如果数组中有包含查询字符串的元素,则返回 true,否则返回 false
function finder(arr, query) {
return arr.some(el => el.includes(query));
}
console.log(finder(arr, 'abc'));
console.log(finder(arr, 'abcd'));
console.log(finder(arr, '123'));
console.log(finder(arr, 'pnq'));
console.log(finder(arr, 'oal'));
希望这对你有所帮助。
英文:
You can use the array method some
, and the string method includes
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = ['pnq', 'dwksnn', 'inabcpi', 'poals'];
// Return true if the array has an element that
// includes the query string, otherwise false
function finder(arr, query) {
return arr.some(el => el.includes(query));
}
console.log(finder(arr, 'abc'));
console.log(finder(arr, 'abcd'));
console.log(finder(arr, '123'));
console.log(finder(arr, 'pnq'));
console.log(finder(arr, 'oal'));
<!-- end snippet -->
答案4
得分: 0
是的,使用.includes()
类似这样:
const arr = ["pnq", "dwksnn", "inabcpi", "poals"]
function check_presence(elem) {
return arr.includes(elem)
}
console.log(check_presence("pnh"))
英文:
Something like this? using the .includes()
?
const arr=["pnq","dwksnn","inabcpi","poals"]
function check_presence(elem) {
return arr.includes(elem)
}
console.log(check_presence("pnh"))
答案5
得分: 0
You can use the includes() method to check.
var arr=["pnq","dwksnn","inabcpi","poals"];
console.log(arr.includes("pnq"))
英文:
we can use the includes() method to check.
var arr=["pnq","dwksnn","inabcpi","poals"];
console.log(arr.indexOf("pnq") > -1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论