0

I have an if-statement that looks like this:

if ("Bolagsmän" in nicerows){
        var contact = this.makeContact(nicerows['Bolagsmän'],true);
    }
    else if ("Komplementär(er)" in nicerows){
        var contact = this.makeContact(nicerows['Komplementär(er)'],true);
    }
    else if("Innehavare" in nicerows){
        var contact = this.makeContact(nicerows['Innehavare'],false);
    }
    else if("Styrelseledamot, verkställande direktör" in nicerows){
        var contact = this.makeContact(nicerows['Styrelseledamot, verkställande direktör'], true);
    }
    else if("Styrelseledamöter" in nicerows){
        var contact = this.makeContact(nicerows['Styrelseledamöter'], true);
    }

As you can see it is a lot of code multiplication just because there are a few possible variables. is there any cleaner way to do this?

Himmators
  • 14,278
  • 36
  • 132
  • 223

3 Answers3

6

You could use a loop, either a modern one:

var names = ["Bolagsmän", "Komplementär(er)", "Innehavare", "Styrelseledamot, verkställande direktör", "Styrelseledamöter"];
var contact;
names.some(function(name)
{
    if (name in nicerows)
    {
        contact = this.makeContact(nicerows[name], name !== 'Innehavare');
        return true; // Breaks the loop
    }
});

Or the boring old kind:

var names = ["Bolagsmän", "Komplementär(er)", "Innehavare", "Styrelseledamot, verkställande direktör", "Styrelseledamöter"];
var contact;
var index, name;
for (index = 0; index < names.length; ++index)
{
    name = names[index];
    if (name in nicerows)
    {
        contact = this.makeContact(nicerows[name], name !== 'Innehavare');
        break;
    }
}

Or possibly put the flag in a map:

var names = {
    "Bolagsmän":                               true,
    "Komplementär(er)":                        true,
    "Innehavare":                              false,
    "Styrelseledamot, verkställande direktör": true,
    "Styrelseledamöter":                       true
};
var name;
var contact;
for (name in names)
{
    if (name in nicerows)
    {
        contact = this.makeContact(nicerows[name], names[name]);
        // Note use of flag -----------------------^
        break;
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Why are you using `.some` here? You're not using the return value. – Benjamin Gruenbaum Aug 06 '13 at 16:21
  • @BenjaminGruenbaum: So I can stop the loop. Insanely, `forEach` doesn't provide a way to do that. – T.J. Crowder Aug 06 '13 at 16:23
  • Got it, I had no idea `.some` has a guarantee over the order of interation, +1 for teaching me something. If only we have nice ES6 support :) `var name = names.find(name=>name in nicerows); contact = this.makeContact(nicerows[name],name!=="Innehavare");` – Benjamin Gruenbaum Aug 06 '13 at 16:26
  • @BenjaminGruenbaum: :-) There's always [cross-compiling](http://code.google.com/p/traceur-compiler/)... – T.J. Crowder Aug 06 '13 at 16:37
0

How about something like this?

var words = {"Bolagsmän": true, "Komplementär(er)": true, "Innehavare": false, "Styrelseledamot, verkställande direktör": true, "Styrelseledamöter": true};

for(var word in words) {
    if(words.hasOwnProperty(word)) {
        if(word in nicerows) {
            var contact = this.makeContact(word, words[word]);
            break;
        }
    }
}
Nadh
  • 6,987
  • 2
  • 21
  • 21
0

When there's only one property in the object anyway, there's no reason to list and try out all the possible names.

for (var name in nicerows)
    var contact = this.makeContact(nicerows[name], name!='Innehavare');
    // break; - not even necessary
Bergi
  • 630,263
  • 148
  • 957
  • 1,375