Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  190] [ 6]  / answers: 1 / hits: 18594  / 10 Years ago, sat, may 10, 2014, 12:00:00

I use the Array map() Method to check each element in an Array and if the condition in the if clausel is true i call another function.



$.map(data['icd'], function (field, i) {
if(field.nummer == search){
Diagnose.single(field.id);
};
});


Now my problem is that i want to stop the map method if a element fullfills the if condition. Because i noticed that when i have for example 6 elements that fullfill the condition the function Diagnose.single(field.id); is called 6 times instead of once!



I tried:



$.map(data['icd'], function (field, i) {
if(field.nummer == search){
Diagnose.single(field.id);
return true;
};
});


But this didnt worked! What can i do instead? Thanks


More From » jquery

 Answers
69

Use simple for-loop with break statement:



for (var i = 0; i < data['icd'].length; i++) {
if (data['icd'][i].nummer == search){
Diagnose.single(data['icd'][i].id);
break;
};
}


map is used for different tasks.


[#71096] Thursday, May 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margob

Total Points: 302
Total Questions: 89
Total Answers: 100

Location: Guadeloupe
Member since Sat, Jul 25, 2020
4 Years ago
;