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

Remove "null" from string in javascript - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Oct 19, 2018 at 12:45 James Monger 10.7k8 gold badges64 silver badges99 bronze badges asked Oct 19, 2018 at 12:38 hacking_mikehacking_mike 1,1511 gold badge11 silver badges27 bronze badges 3
  • 4 returnValue.replace('null'," ");? Replace the null object, with null as a string – BritishWerewolf 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'? – Rence Commented Oct 19, 2018 at 12:40
  • @Sirence He'll bee 'Joe mann' and may finally get a value out of his name – D.Schaller Commented Oct 19, 2018 at 14:00
Add a ment  | 

7 Answers 7

Reset to default 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(" ");
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.

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

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

try this

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

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

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

return returnValue 

Try ternary operator for null avoiding:

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

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)

发布评论

评论列表(0)

  1. 暂无评论