0

What is the correct way of breaking out of a nested if statement/for loop? I have tried the following approach but the preferred approach does not work:

service.js - NOT WORKING but better because it breaks out as soon as a match is found

    getSelectedService: function(serviceId) {
        serviceId = parseInt(serviceId);
        for(i=0;i<servicesData.length;i++) {
            if(servicesData[i].id === serviceId) {
                var service = servicesData[i];
                return service;
            }
        }
    }

services.js - WORKING but not good as it loops through everything even when a match is found

getSelectedService: function(serviceId) {
    serviceId = parseInt(serviceId);
    servicesData.forEach(function(service) {
        if(service.id === serviceId) {
            var selectedService = service;                  
        }
    });
    return selectedService;
}
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • 1
    The first code returns the first matched service, the second returns the last matched service. It's not obvious what "NOT WORKING" means. They are equivalent and most likely identical. – zerkms Jan 18 '16 at 03:28
  • Use `filter`. `getSelectedService: function(serviceId) { serviceId = parseInt(serviceId); var service = servicesData.filter(service => service.id === serviceId); return service.length ? service[0] : {}; }` – Tushar Jan 18 '16 at 03:29
  • @Tushar why would one prefer your suggestion over the first code example? And as soon as you use ES2015 - why not use `Array.prototype.find()`? – zerkms Jan 18 '16 at 03:30
  • @zerkms Thanks for the response. I asked a question earlier on regarding this: http://stackoverflow.com/questions/34845292/share-data-between-controllers-in-ionic-framework-angular-js. I hope it may provide more context. – methuselah Jan 18 '16 at 03:31
  • @methuselah no it does not - what "NOT WORKING" means? Those 2 implementations are equivalent and work the same. If you tried to run them in isolation you would see that. – zerkms Jan 18 '16 at 03:32
  • @zerkms I am trying to use method getSelectedService to search ServicesData for a particular id and inject ServicesData to ServiceCtrl, which then appears on a different page. When I say not working, it means that I do not get the data appearing on the second page (service.html). – methuselah Jan 18 '16 at 03:33
  • 1
    https://jsfiddle.net/q4s85vfm/ --- your first code works for sure. – zerkms Jan 18 '16 at 03:33
  • duplicate:http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break – Ammaroff Jan 18 '16 at 03:34
  • "it means that I do not get the data appearing on the second page" --- the problem is somewhere else then, both your code samples are identical and both work fine. – zerkms Jan 18 '16 at 03:34
  • 1
    @zerkms you're right. I looked elsewhere and found the problem – methuselah Jan 18 '16 at 03:37

1 Answers1

1

If you want to stop on the first match, you shoud use a while loop.

var keepGoing = true;
var i = 0;
var service;

while( keepGoing && i < servicesData.length ) {
    if( servicesData[i].id === serviceId ) {
        service = servicesData[i];
        keepGoing = false;
    } else {
        i++;
    }
}

return service;
  • 1
    @methuselah - There's no fundamental reason a `while` loop is better than a `for` loop for this purpose. The two can be used fairly interchangeably. You can break out of a `for` loop with `break` or `return` just fine. – jfriend00 Jan 18 '16 at 03:44
  • I agree, I just gave an answer different from the solutions he tried. – Corentin Bruneau Jan 18 '16 at 03:47