-3

i am facing a confused how can i terminate a loop when i get the correct value from iteration. I have pasted a code snippet below. What i am trying to do is that i have one string called skillID, and other array called emplSkillLevel. When skillID does match in the loop, then it returns the string after setting the value in string but at some points loop doesn't break it.

How can i terminate it?

setCellValue: function(skillID,emplSkillLevel){ 
           var cellVal=0;
           $.each(emplSkillLevel, function(index1, item1) {
               var emplSkillLevelID = emplSkillLevel[index1].split("-");
               if(emplSkillLevelID[0] == skillID){
                   cellVal = emplSkillLevelID[1];
                   return cellVal;
                   break;
               }
               else{
                   cellVal=0;
                   return cellVal;
               }

           });

       }
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
user1030128
  • 411
  • 9
  • 23
  • return already exits the function, so you can not execute anything afterwards. As for how to break out of each loops, see documentation: http://api.jquery.com/each/ – CBroe Jul 01 '13 at 10:16
  • 2
    why did this get so many negative votes? – Oliver Watkins Jul 01 '13 at 10:19
  • Probably because "Lets use jQuery for everything without even knowing what does it do". - That said i didn't downvote this, but that might be the reason. – Robert Aug 04 '15 at 13:58

4 Answers4

4

To break a jQuery $.each loop you need to return false; instead of break. When you return a non-false value this actually means continue;

Also, you cannot use return cellVal; and after that break; - the return already cuts execution.

Try using $(emplSkillLevel).each(...) instead of $.each(emplSkillLevel,...).

.each() docs

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
1

You're returning from the internal function you pass to $.each.

If you don't want to iterate on all elements, the best is to use the native forEach or a simple loop :

 setCellValue: function(skillID,emplSkillLevel){ 
       for (var index1=0; index1<emplSkillLevel.length; index1++) {
           var item1 = emplSkillLevel[index1];
           var emplSkillLevelID = emplSkillLevel[index1].split("-");
           if(emplSkillLevelID[0] == skillID){
               return emplSkillLevelID[1];
           }
       }
       return 0;
   }

EDIT : I forgot you can use return false as mentionned by Yotam, his answer is good too.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

You can not return value from $.each. You need global variable to store founded value. How to break How to break/exit from a each() function in JQuery?

Community
  • 1
  • 1
Petr B.
  • 879
  • 1
  • 8
  • 17
0

The answers provided by others here should help you as well. This is my take on it but you will have to make some changes.

var setCellValue =  function(skillID, emplSkillLevels){ 
    var cellVal = 'None';

    $.each(emplSkillLevels, function(index, item) {
        var arrSkillLevel = emplSkillLevels[index].split("-");

        if(arrSkillLevel[0] == skillID){
          cellVal = arrSkillLevel[1];

          return false;
        }
    });

    return cellVal;
}

var emplSkillLevels = ["1-Basic", "2-Intermediate", "3-Advanced"];

console.log(setCellValue(2, emplSkillLevels));
manraj82
  • 5,929
  • 24
  • 56
  • 83