Marco Santos
Marco Santos

Reputation: 1005

returning function instead of value

im tring to format some values and also creating a if statement in case that the value is null, but is returning me a function instead of a value.

Here is my code:

var data = _.map(information).map(function(x) {
  return _.assign(x, {
    age: function() {
      if (x.age != null) {
        x.age.toString();
      }
    }
  });
});

Upvotes: 1

Views: 3098

Answers (1)

Lukasz Wiktor
Lukasz Wiktor

Reputation: 20422

Invoke your function inline if you want to get the result (and don't forget to add return):

age: (function() {
    if (x.age != null) {
        return x.age.toString();
    }
})()

However you don't need a function to check if value isn't null. You can write a boolean expression:

age: x.age && x.age.toString()

or use a ternary operator:

age: x.age ? x.age.toString() : undefined

Upvotes: 3

Related Questions