2

In one of my previous question js: Multiple return in Ternary Operator I asked about returning multiple parameter with ternary-operator. But now the parameter IsActveUser boolean(true|false) flag have been modified to integer(0|1|2|3) flag,

Previously it has only two states,

True - Valid User
False - InValid User

now it has four states,

0 - InValid User
1 - Valid User
2 - Future User
3 - Expired User

So previously, I have coded like,

var data = userInfo.map(function (element) {
    return {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'IsActiveUser': ~element[9].search("Valid") ? "True": "False" // really a string?
    };
)}

Now I have to check like this, for four status flag

var data = userInfo.map(function (item) {
    if (item[9].search("title = \"Expired\"")) {
        return {
            'LastName': Capitalizefirstletter(item[1]),
            'FirstName': Capitalizefirstletter(item[2]),
            'UserName': item[3],
            'UserStatusFlag': 3 // flag should be 3 in this case
        };
    }
    else if (item[9].search("title = \"Valid\"")) {
        return {
            'LastName': Capitalizefirstletter(item[1]),
            'FirstName': Capitalizefirstletter(item[2]),
            'UserName': item[3],
            'UserStatusFlag': 1 // flag should be 1 in this case
        };
    }
    else if (item[9].search("title = \"Invalid\"")) {
        return {
            'LastName': Capitalizefirstletter(item[1]),
            'FirstName': Capitalizefirstletter(item[2]),
            'UserName': item[3],
            'UserStatusFlag': 0 // flag should be 0 in this case
        };
    }
    else if (item[9].search("title = \"Future\"")) {
        return {
            'LastName': Capitalizefirstletter(item[1]),
            'FirstName': Capitalizefirstletter(item[2]),
            'UserName': item[3],
            'UserStatusFlag': 2 // flag should be 2 in this case
        };
    }
});

but this is not working as expected in Knockout JS.

Shall I use anything like this, else-if checking inside return satement

var data = userInfo.map(function (element) {
    return {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'UserStatusFlag': if(~element[9].search("Valid")) {1}
                                    else if (~element[9].search("InValid")) {0}
                                    else if (~element[9].search("Future")) {2}
                                    else if (~element[9].search("Expired")) {3}
    };
})

Any suggestion would be helpful.

Community
  • 1
  • 1
Jeya Suriya Muthumari
  • 1,947
  • 3
  • 25
  • 47

3 Answers3

4

Make a function call, use a switch statement

function getCode (item) {
    switch (item) {
        case "InValid User" : 
            return 0;
        case "Valid User" :
            return 1;
        case "Future User" :
            return 2;
        case "Expired User" :
            return 3;
    }
}

and call it when you build the object

var data = userInfo.map(function (element) {
    return {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'UserStatusFlag': getCode(element[9])
    };
});

or you can use an object instead of a switch

var states = {
  "InValid User" : 0,
  "Valid User" : 1,
  "Future User" : 2,
  "Expired User" : 3
};

var data = userInfo.map(function (element) {
    return {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'UserStatusFlag': states[element[9]]
    };
});
epascarello
  • 204,599
  • 20
  • 195
  • 236
4

I detest switch and if blocks for this. An object mapping works so much better:

var USER_LOOKUP = {
    "InValid User" : 0,
    "Valid User" : 1,
    "Future User" : 2,
    "Expired User" : 3
};

var data = userInfo.map(function (element) {
    return {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'UserStatusFlag': USER_LOOKUP[element[9]]
    };
)};
IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
2

In JavaScript if is not an expression so it does not have a return value. But you can use the ternary operator (it's acctually called conditional operator in JavaScript)

I assume that the reason why your code is not working is that String.prototype.search returns the index of the found string or -1 if the string was not found. Depending on your data you might not need to use search and can just compare the sting directly. So instead of

item[9].search("title = \"Valid\"")

you have to use

item[9].search("title = \"Valid\"") > -1

or you can do what you did in your 2nd example and use the tilde (as explained here)

~item[9].search("title = \"Valid\"")

So you could use:

item[9].search('title = "Expired"') > -1 ? 3 :
item[9].search('title = "Valid"')   > -1 ? 1 :
item[9].search('title = "Invalid"') > -1 ? 0 :
item[9].search('title = "Future"')  > -1 ? 2 :
(()=> { throw Error(item[9] + ' is not a valid state' ) })()

Of course a switch or a map might also work.

Community
  • 1
  • 1
Till Arnold
  • 561
  • 2
  • 11
  • That's Really Great! It Worked. Considering the code efficiency and code line matters I will go with this. – Jeya Suriya Muthumari Jun 07 '16 at 06:25
  • Is it really more efficient to do multiple `if`s versus an object lookup? – IMTheNachoMan Jun 08 '16 at 20:28
  • I do not exactly know how expensive an object lookup is compared to multiple `if`s. But I am not sure if there is a valid solution using an object lookup for this question. In the question `String.prototype.search` is used and depending on the data in element[9] this might be necessary. – Till Arnold Jun 08 '16 at 21:28