3

I have this code:

var returnValue = item.fname + " " + item.mname + " " + item.lname
returnValue.replace(null," ");
return returnValue ;

Sometimes one of the fields is null so returnValue is:

"John null Doe"

or

"John something null"

I want to get rid of the "null" but my code does not seem to work.

Can someone help me out here?

3
  • 4
    returnValue.replace('null'," ");? Replace the null object, with null as a string Commented Oct 19, 2018 at 12:40
  • 3
    Instead of removing null afterwards, add a check to your fields and only add them if they are not null to avoid the issue alltogether. Otherwise, what would happen if someones name is 'Joe Nullmann'? Commented Oct 19, 2018 at 12:40
  • @Sirence He'll become 'Joe mann' and may finally get a value out of his name Commented Oct 19, 2018 at 14:00

7 Answers 7

4

Rather than replacing null afterwards, only append the individual names if they are not null.

var returnValue = "";

if (item.fname !== null) {
    returnValue += item.fname + " ";
}

if (item.mname !== null) {
    returnValue += item.mname + " ";
}

if (item.lname !== null) {
    returnValue += item.lname;
}

return returnValue;

Alternatively, use Array.prototype.filter to remove nulls:

// store the names in an array
var names = [ item.fname, item.mname, item.lname ];

// filter the array to values where they are `!== null`
var notNullNames = names.filter(x => x !== null);

// join them with spaces
var returnValue = notNullNames.join(" ");
Sign up to request clarification or add additional context in comments.

Comments

1
var returnValue = (item.fname || " ") + " " + (item.mname || " ") + " " + (item.lname || " ");
return returnValue;

Be careful with mixing var types (string and null for example). Better make sure the variable is set or has a fallback.

Comments

1

I'd recommend you another technique: place your string parts to array, filter it and join it:

[item.fname, item.mname, item.lname].filter(v => !!v).join(' ')

5 Comments

you could filter with Boolean as cb.
@NinaScholz it's not recommended, as filter callback receives 3 arguments: currentElement, index, array while Boolean accepts just 1.
"it's not recommended" - Why? Boolean() will just ignore the additional arguments
@Andreas that's the problem of JS and that's why big companies are using Flow and TypeScript. It's misuse of API. If it accepts a single param then you should pass only a single param
but you are using only one parameter as well.
0

try this

>    return (item.fname ? item.fname : '')  + " " + (item.mname ? item.mname : '') + " " + (item.lname ? item.lname : '');

1 Comment

This one did it for me. Thanks
0
var returnValue = item.fname

if(item.mname)returnValue  += " " + item.mname

 if(item.lname)returnValue  += " " + item.lname

return returnValue 

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
0

Try ternary operator for null avoiding:

var returnValue = item.fname + " " + item.mname ? item.mname : "" + " " + item.lname

Comments

0

In my opinion, the most elegant solution is:

[item.fname, item.lname, item.lname].join(' ');

For example:

const item = {}
item.fname = 'foo'
item.lname = 'bar'

console.log([item.fname, item.mname, item.lname].join(' '))

Otherwise you can use the or operator to skip falsy objects:

const item = {}
item.fname = 'foo'
item.lname = 'bar'

const joined = (item.fname || '') + " " + (item.mname || '') + " " + (item.lname || '')

console.log(joined)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.