arr = arr.some((item) => { return item.includes(/^.*active.*$/); });这样写如何修改
时间: 2024-02-16 17:04:17 浏览: 30
如果您想使用 `includes` 方法来判断数组中是否包含符合正则表达式的字符串,可以使用以下代码:
```javascript
const regex = /active/;
const arr = ['dark-anchor-link-title', 'dark-anchor-link-title-active'];
const result = arr.some(item => regex.test(item));
console.log(result); // true
```
在这里,我们使用了正则表达式 `regex` 来匹配是否包含 `active`,然后使用 `test` 方法来判断每个元素是否符合正则表达式。最后使用 `some` 方法来判断数组中是否有符合条件的元素。
相关问题
watch: { dataList(val) { let arr = JSON.parse(JSON.stringify(val)) arr.map((ele, index) => { this.saveRows.map((item) => { if (item.containerId && ele.containerId) { let arr1 = ele.containerId, arr2 = item.containerId let ifHas = arr1.every((a) => { return arr2.some((b) => a === b) }) //如果是在这个里面,那么就是true console.log(ifHas) //true if (ifHas) { this.$nextTick(() => { this.$refs.table.toggleRowSelection(this.dataList[index], true) }) } } }) }) if (this.cancelRows.length > 0) { arr.map((ele, index) => { this.cancelRows.map((item) => { if (item.containerId && ele.containerId) { let arr1 = ele.containerId, arr2 = item.containerId let ifHas = arr1.every((a) => { return arr2.some((b) => a === b) }) if (ifHas) { this.$nextTick(() => { this.$refs.table.toggleRowSelection( this.dataList[index], false, ) }) } } }) }) } },
这段代码是一个Vue组件的`watch`属性中的一个监听函数。它监听`dataList`属性的变化,并在值发生变化时执行一些操作。
首先,它将`val`的值深拷贝到`arr`数组中,以防止对原始值的修改。
然后,它使用`map`方法遍历`arr`数组中的每个元素,并在每个元素上执行一些操作。在这个操作中,它使用`this.saveRows`数组中的每个元素来与当前元素进行匹配。
如果当前元素和`this.saveRows`数组中的元素都有`containerId`属性,那么它会比较两个数组的`containerId`属性是否完全相同。如果完全相同,它会将当前元素添加到Vue组件中的一个表格组件的选中行中。
接着,它检查是否存在`this.cancelRows`数组中的元素,并与当前元素进行匹配。如果匹配成功,它会将当前元素从Vue组件中的表格组件的选中行中移除。
总之,这段代码的目的是根据特定的条件来更新Vue组件中的一个表格组件的选中行。
async function confirm() { isShow.value = false; // params.value.pageSize = 7 let i = menu.value.find((v) => v.isActive === true).id - 1; if (i === 0) { params.value.houseType = tagsList.value.find( (v) => v.isActive === true )?.value; } else if (i === 1) { params.value.orientation = tagsList.value.find( (v) => v.isActive === true )?.value; } else if (i === 2) { params.value.rentStr = tagsList.value.find( (v) => v.isActive === true )?.value; } else if (i === 3) { let arr = []; tagsList.value?.forEach((v) => { if (v.isActive === true) { arr.push(v.value * 1); } }); if (arr.length > 0) { params.value.houseLabels = arr.join(","); } } search(); }优化
可以将这段代码优化为:
```
isShow.value = false;
// params.value.pageSize = 7
const i = menu.value.findIndex(v => v.isActive) - 1;
if (i === 0) {
params.value.houseType = tagsList.value.find(v => v.isActive)?.value;
} else if (i === 1) {
params.value.orientation = tagsList.value.find(v => v.isActive)?.value;
} else if (i === 2) {
params.value.rentStr = tagsList.value.find(v => v.isActive)?.value;
} else if (i === 3) {
const arr = tagsList.value?.filter(v => v.isActive)?.map(v => v.value * 1);
if (arr?.length) {
params.value.houseLabels = arr.join(",");
}
}
search();
```
这样可以将代码变得更加简洁和易读,同时也可以避免使用可选链操作符时的一些潜在问题。