最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Java Script: Nested Return Statement, return inside Return - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Jun 6, 2016 at 13:28 Jeya Suriya MuthumariJeya Suriya Muthumari 2,0413 gold badges30 silver badges50 bronze badges 3
  • 2 Why not make it a function call with a simple switch that returns a value? – epascarello Commented Jun 6, 2016 at 13:33
  • 1 maybe you could add some examples of element? – Nina Scholz Commented Jun 6, 2016 at 13:33
  • How exactly is it not working as expected? What are example values of item[9]? Is it item[9] or element[9]? – Sebastian Simon Commented Jun 6, 2016 at 13:38
Add a ment  | 

3 Answers 3

Reset to default 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]]
    };
});

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]]
    };
)};

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 pare 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.

发布评论

评论列表(0)

  1. 暂无评论