使用 any 将 for 循环替换为 linq
Replace for loop with linq using any
我在打字稿中有以下代码:
public executeTest(test: Test): void {
const testFilters: Record<string> = getTestFilters();
let isTestingRequired: boolean = false;
for (let i: number = 0; i < testFilters.length; i++) {
if(test.Name === testFilters[i].Name){
isTestingRequired = true;
break;
}
}
}
我正在尝试使用 LINQ 替换上面的 for 循环,但出现错误。
let isTestingRequired: boolean = testFilters.any((filter.): boolean => {
return filter.Name === test.Name
});
您需要先使用 Enumerable.from()
使您的 array
可枚举,如下所示:
var array = [{name: "John"}, {name: "Test"}];
var result = Enumerable.from(array)
.any(obj => obj.name == "Test");
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/3.2.4/linq.min.js"></script>
我在打字稿中有以下代码:
public executeTest(test: Test): void {
const testFilters: Record<string> = getTestFilters();
let isTestingRequired: boolean = false;
for (let i: number = 0; i < testFilters.length; i++) {
if(test.Name === testFilters[i].Name){
isTestingRequired = true;
break;
}
}
}
我正在尝试使用 LINQ 替换上面的 for 循环,但出现错误。
let isTestingRequired: boolean = testFilters.any((filter.): boolean => {
return filter.Name === test.Name
});
您需要先使用 Enumerable.from()
使您的 array
可枚举,如下所示:
var array = [{name: "John"}, {name: "Test"}];
var result = Enumerable.from(array)
.any(obj => obj.name == "Test");
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/3.2.4/linq.min.js"></script>