Skip to content Skip to sidebar Skip to footer

Js: Multiple Return In Ternary Operator

For my task, I have a done a piece of code like to fetch the user details, var data = userInfo.map(function (element) { if (element[9].search('Active') != -1) { return

Solution 1:

Since the difference is only one property (IsActiveUser), you can insert the comparison inside the object.

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

Solution 2:

Your map function needs to return something on each iteration. Just add the return keyword:

var data = userInfo.map(function (element) {
    return (element[9].search("Active")) != -1  ? {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'IsActiveUser': "True"
    } : {
        'LastName': Capitalizefirstletter(element[1]),
        'FirstName': Capitalizefirstletter(element[2]),
        'UserName': element[3],
        'IsActiveUser': "False"
    };
}

Post a Comment for "Js: Multiple Return In Ternary Operator"