0

I am using replace method to replace some text for example:-

let data = "My name is [[employeeName]]";

data.replace('[[employeeName]]', this.employee.employeeName);

Now my concern is the employee object which can be null, if it is null then above code will fail. So I want to put the condition some thing like this:

{this.employee ? this.employee.employeeName : ''}

How can I put a condition inside the replace() method?

Please assist me

3
  • Replace with employeeName or replace it with empty string? Commented Feb 22, 2018 at 9:22
  • 1
    data.replace('[[employeeName]]', (this.employee.employeeName==null)?'':this.employee.employeeName); Commented Feb 22, 2018 at 9:23
  • Uh, just place exactly what you wrote inside the replace argument?! data.replace('[[employeeName]]', this.employee ? this.employee.employeeName : ''); Commented Feb 22, 2018 at 9:33

3 Answers 3

3

Use

(this.employee && this.employee.employeeName) || "";

i.e.

data.replace('[[employeeName]]', (this.employee && this.employee.employeeName) || "" );

Note

  • This will replace [[employeeName]] with "" when either of this.employee and this.employee.employeeName is undefined.

i.e., if your object is

var obj = {};
data.replace('[[employeeName]]', (obj.employee && obj.employee.employeeName) || "" ); //will still replace [[employeeName]] with ""

and

var obj = { employee : {} };
data.replace('[[employeeName]]', (obj.employee && obj.employee.employeeName) || "" ); //will still replace [[employeeName]] with ""

But if employeeName is present

var obj = { employee : { employeeName : "emp" } };
data.replace('[[employeeName]]', (obj.employee && obj.employee.employeeName) || "" ); //will replace [[employeeName]] with "emp"
Sign up to request clarification or add additional context in comments.

4 Comments

Why not use the conditional operator as the OP had it in his question?
@Bergi OP's condition didn't handle this.employee.employeeName being undefined and handling it via conditional operator would have made the code longer, so I chose this approach.
Right, but I think his concern was only about this.employee being null, not about an existing employee having no name.
@Bergi In that case, replacement would have happened with undefined and I thought it wouldn't be a desirable scenario. I left a note as well explaining the consequence of using this solution to the OP.
2

The simplest way would be to just add an 'OR' evaluation with a check that employee object is also not null :

data.replace('[[employeeName]]', this.employee ? this.employee.employeeName || "" : "");

Which will replace with empty string if employee is null, or employee name is null.

Comments

0

If you really want to use a ternary expression :

let name = this.employee? this.employee.employee.name: '';
let data = "My name is [[employeeName]]";
data.replace('[[employeeName]]', name);

or just put your code in an if else statement :

Also you don't actually need the replace function if you do so :

if(!(typeof employee ==="undefined")){
       let data= "My name is "+ this.employee.employeeName;
    }
    else{
      let data = "My name is nobody";
    }

2 Comments

inside the replace i want to apply the condition like this data.replace.('[[employeeName]]', {this.employee? this.employee.employeeName: ''});
In my opinion, you shouldn't. Code readability is important and by trying to put too much stuff in one single instruction you just make it harder to read without especially gaining anything back.

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.