如何检查:数组是否正确包含特定文本?
how to check: if array includes specific text properly?
我正在努力进行数组搜索,其中包含一段必须包含反斜杠的文本。我已经尝试 includes('');
否定 includes('');
并尝试使用 indexOf('')
.
进行类似的尝试
我有一个简单的数组,最多有四个值;通常它有两个,这是它通常的样子:
{tks: Array(2)}
tks: Array(2)
0: "https://mycoolwebsite.net/arcgis/rest/services"
1: "https://mycoolwebsite.com/arcgis/rest/services"
length: 2
__proto__: Array(0)
__proto__: Object
以下是我尝试执行的简单检查:我对 *includes*
'wdt'
文本的第二次检查似乎有效,所以 我认为它是带有反斜杠的东西。无论如何我可以处理这个? 我很困惑为什么我的 if
和 else
都在下面使用反斜杠的第一次检查中被击中......我添加了否定 else
仔细检查.. 在 else
中有和没有它,else
总是被击中。
// right before doing the search I am building the array, just to add more context
for (let i = 0; i < coolObj.length; i++) {
if (coolObj[i].url){
tr = coolObj[i].url;
tks.push(tr);
console.log({tks});
}
}
console.log({tks}); // consoles as I have included above ^
if (tks.includes('/arcgis/rest/services')) {
console.log({tks});
} else if (!tks.includes('/arcgis/rest/services')) {
console.log({tks});
console.log('does not include correct url');
aT = '/arcgis/rest/services';
lP = false;
filterAt(aT);
}
if (tks.includes('wdt')) {
console.log({tks});
}else {
console.log({tks});
wT = 'wdt';
filterWt(wT);
}
来自MDN docs:includes() 方法确定数组的条目中是否包含某个值,并根据需要返回 true 或 false。
您必须在数组元素中使用 String.prototype.includes
测试字符串,因此:
const arr = ["https://mycoolwebsite.net/arcgis/rest/services", "https://mycoolwebsite.com/arcgis/rest/services"];
arr.forEach(el => {
if (el.includes('/arcgis/rest/services')) {
console.log(el, ': includes url');
}
else {
console.log('does not include correct url');
}
if (el.includes('wdt')) {
console.log(el, ': includes wdt');
} else {
console.log('does not include correct wdt');
}
});
我正在努力进行数组搜索,其中包含一段必须包含反斜杠的文本。我已经尝试 includes('');
否定 includes('');
并尝试使用 indexOf('')
.
我有一个简单的数组,最多有四个值;通常它有两个,这是它通常的样子:
{tks: Array(2)}
tks: Array(2)
0: "https://mycoolwebsite.net/arcgis/rest/services"
1: "https://mycoolwebsite.com/arcgis/rest/services"
length: 2
__proto__: Array(0)
__proto__: Object
以下是我尝试执行的简单检查:我对 *includes*
'wdt'
文本的第二次检查似乎有效,所以 我认为它是带有反斜杠的东西。无论如何我可以处理这个? 我很困惑为什么我的 if
和 else
都在下面使用反斜杠的第一次检查中被击中......我添加了否定 else
仔细检查.. 在 else
中有和没有它,else
总是被击中。
// right before doing the search I am building the array, just to add more context
for (let i = 0; i < coolObj.length; i++) {
if (coolObj[i].url){
tr = coolObj[i].url;
tks.push(tr);
console.log({tks});
}
}
console.log({tks}); // consoles as I have included above ^
if (tks.includes('/arcgis/rest/services')) {
console.log({tks});
} else if (!tks.includes('/arcgis/rest/services')) {
console.log({tks});
console.log('does not include correct url');
aT = '/arcgis/rest/services';
lP = false;
filterAt(aT);
}
if (tks.includes('wdt')) {
console.log({tks});
}else {
console.log({tks});
wT = 'wdt';
filterWt(wT);
}
来自MDN docs:includes() 方法确定数组的条目中是否包含某个值,并根据需要返回 true 或 false。
您必须在数组元素中使用 String.prototype.includes
测试字符串,因此:
const arr = ["https://mycoolwebsite.net/arcgis/rest/services", "https://mycoolwebsite.com/arcgis/rest/services"];
arr.forEach(el => {
if (el.includes('/arcgis/rest/services')) {
console.log(el, ': includes url');
}
else {
console.log('does not include correct url');
}
if (el.includes('wdt')) {
console.log(el, ': includes wdt');
} else {
console.log('does not include correct wdt');
}
});