将通过一个示例说明此问题。我试图为此使用includes命令,但这没有用,您将在一秒钟内看到原因。
我有2个变量:
var Tutor = "Physics, English, History"; /* subjects that a tutor can teach */
var Learner = "Physics, History"; /* subjects in wich a learner want to be tutored */
当我使用命令时:
var n = tutor.includes(Learner);
由于顺序(我想),结果为“假”。但是,我希望结果是“正确的”,因为这名导师的科目包括物理和历史。可悲的是,我无法更改主题的顺序,也无法删除逗号,因为该数据基于来自Google表单的
。非常感谢您的帮助:)
答案 0 :(得分:1)
如果通过拆分将数据解析为数组,则更加容易
const parse = data => data.split(", ");
然后,您可以只使用.every
和.includes
:
parse(Learner).every(learner => parse(Tutor).includes(learner)) // true
答案 1 :(得分:0)
基本上使用replace
,split
和every
,您可以得到想要的结果。
var Tutor = "Physics, English, History";
var Learner = "Physics, History";
function checkIfContains(stringTocheck, stringToMatch) {
//lets clean the spaces so we can have a better approach.
const withoutSpaces = stringToMatch.replace(' ', '');
const arrayFromString = withoutSpaces.split(',');
//user EVERY to check that everything is included
const result = arrayFromString.every(entry => stringTocheck.includes(entry))
return result;
}
const contains = checkIfContains(Tutor, Learner);
console.log(contains)
答案 2 :(得分:0)
尝试通过用逗号将学习者字符串分割成一个数组,
然后使用every()
函数遍历新创建的数组,然后检查导师中是否存在每个单词
字符串,如果出现每个单词,则返回true,否则返回false
var Tutor = "Physics, English, History"; /* subjects that a
the tutor can teach */
var Learner = "Physics, History"; /* subjects in wich a
learner want to be tutored */
var learnerArray = Learner.split(',');
let result = learnerArray.every( word =>
Tutor.includes(word));
console.log(result)
答案 3 :(得分:0)
这超出了您的问题,但我认为这是正确的方向。您的两个变量都代表我认为可以变化的主题列表。因此,它们最好是字符串数组而不是字符串数组:
var Tutor = ["Physics", "English", "History"]; /* subjects that a tutor can teach */
var Learner = ["Physics", "History"]; /* subjects in which a learner want to be tutored */
现在,您可以检查Tutor变量是否包含与Learner相同的主题,而无需考虑顺序(这是您所关心的)。
这可以通过遍历Learner数组中的所有元素并检查Tutor数组是否包含它们来完成:
function TutorContainsAllSubjects() {
var Tutor = ["Physics", "English", "History"];
var Learner = ["Physics", "History"];
var i;
for (i = 0; i < Learner.length; i++) {
if (!Tutor.includes(Learner[i])) {
alert(false);
return false;
}
}
alert(true);
}
<html>
<head></head>
<body>
<button onclick="TutorContainsAllSubjects()">Click me</button>
</body>
</html>
答案 4 :(得分:0)
逻辑:
var Tutor = "Physics, English, History";
var Learner = "Physics, History";
function contains(csvList, csvSubList) {
let aList = csvList.replace(/ /g, '').split(',');
let aSubList = csvSubList.replace(/ /g, '').split(',');
return aSubList.every( item => aList.includes(item) );
}
let result = contains(Tutor, Learner);
console.log( result );
// example of failure.
result = contains( Tutor, 'Physics, French' );
console.log( result );
答案 5 :(得分:-2)
您可以使用indexOf()
,该函数返回正在寻找的字符串在寻找的字符串中的索引位置。如果未找到子字符串,则indexOf()
返回{{1 }}。
-1
但是,在您的情况下,与其对用逗号分隔的潜在字符串值进行字符串测试,不如设置一个受教的主题数组,然后使用var Tutor = "Physics, English, History"; /* subjects that a tutor can teach */
var Learner = "Physics, History"; /* subjects in wich a learner want to be tutored */
var n = Tutor.indexOf(Learner) > -1;
console.log(n);
搜索该数组进行比赛:
indexOf()