I have the following nested for loop in my code. this works fine but i am not sure whether this is a good way to do it.i would like to know what is the best optimised way to handle such cases in typescript to get rid of for loops.
for (let j = 0; j <= list.length - 1; j ) {
const student = list[j];
for (let i = 0; i <= selectedStudent.subs.length - 1; i ) {
const id = selectedStudent.subs[i].id;
if(student[id] === id){
console.log(id);
}
}
}
CodePudding user response:
There is no "typescript" or even "javascript" way to optimize a nested loop. The question is about algorithmic complexity. If you have to search selectedStudent.subs for each student, then you have to.
CodePudding user response:
if you are just looking to eliminate the for loop from your code, you can use the Array prototype methods such as map, forEach, filter, etc... to achieve the same results.
check the methods on the Array.prototype for more information https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
