在两个元素 JavaScript 中查找匹配项
我们需要编写一个函数,如果数组第一个元素中的字符串包含数组第二个元素字符串的所有字母,则返回 true。
例如,
["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case.
参数 ["hello", "hey"] 应返回 false,因为字符串 "hello" 不包含 "y"。
最后,["Alien", "line"],应返回 true,因为 "line" 中的所有字母都出现在 "Alien" 中。
这是一个相当简单的问题;我们只需分割数组的第二个元素,并遍历由此产生的数组,以检查第一个元素是否包含所有字符。
示例
const arrayContains = ([fist, second]) => {
return second
.toLowerCase()
.split("")
.every(char => {
return fist.toLowerCase().includes(char);
});
};
console.log(arrayContains(['hello', 'HELLO']));
console.log(arrayContains(['hello', 'hey']));
console.log(arrayContains(['Alien', 'line']));输出
控制台中的输出将是 −
true false true
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP