Javascript conditional return

let obj = {

  true1: true,

  true2: true,

  true3: true,

};

let true1 = true;

let true2 = true;

let true3 = true;

if (true1) {

  return obj.true1;

}

if (true1 && true2) {

  return obj.true1 && obj.true2;

}

if (true2 && true3) {

  return obj.true2 && obj.true3;

}

if (true1 && true3) {

  return obj.true1 && obj.true3;

}

if (true1 && true2 && true3) {

  return obj.true1 && obj.true2 && obj.true3;

}

I want this to happen in a short way.
Instead of checking one by one. Just wanna store those return variables in a data lets call as X.
And finally wanna return that final variable X just like => return X
I wanted to demonstrate a bit what I am trying to explain in below code.
(Which is not a right way to do that.) I wanna learn what is actually the right way ?

let returnvalue;

if (true1) {

  returnvalue += obj.true1;

}

if (true2) {

  returnvalue += obj.true2;

}

if (true3) {

  returnvalue += obj.true2;

}

return returnvalue; (wanted to achieve to returnvalue such as : 
returnvalue ==> obj.true1 && obj.true2 && obj.true3)

And just do not get bothered with the variable names. I created them so that I can explain my problem in more clear way.

I know if it was the string case I would do that just like ;

let returnValue ="";

if(true){
returnValue+="aValue"
}
if(true}{
returnValue+="bValue"
}

return returnValue;

But since return variables are not string, how to do combine them in proper way ?

I am confused at one thing and wanted to ask to you guys. My concern is in code comments. Thanks in advance.

Are you asking about &=

Hey there. Thanks for answering. But no. I wanted to learn how to combine return values which are not String type. I mean I know if return values were to be string, I could do that like below.

let returnValue ="";

if(true){
returnValue+="aValue"
}
if(true}{
returnValue+="bValue"
}

return returnValue;

Combine how though? You can add a variable that you will return just like you can for any other variable. You can and it, or it, or anything else you can do with a variable.

let returnvalue;

if (true1) {

  returnvalue += obj.true1;

}

if (true2) {

  returnvalue += obj.true2;

}

if (true3) {

  returnvalue += obj.true2;

}

return returnvalue; (wanted to achieve to returnvalue such as : 
returnvalue ==> obj.true1 && obj.true2 && obj.true3)

By combining I meant that;

return obj.true1 && obj.true2 && obj.true3

So… You can use the and operator just fine

valueToReturn = valueToReturn && otherValue

Okay I guess my example did not explain enough what I was trying to explain. Sorry for that. I will share the actual code that I ve been working on.

I am trying to do in memory search based on input value. Again you better do not bother with the variable names. Since they are a bit confusing and related to database keys.

    {studentData
                      .filter(student => {
                        if (
                          searchTerm.name !== '' &&
                          searchTerm.department !== '' &&
                          searchTerm.phone !== '' &&
                          searchTerm.studentno !== ''
                        ) {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department) &&
                            student.userId.phone.includes(searchTerm.phone) &&
                            student.studentno.includes(searchTerm.studentno)
                          );
                        } else if (
                          searchTerm.name !== '' &&
                          searchTerm.department !== '' &&
                          searchTerm.phone !== ''
                        ) {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department) &&
                            student.userId.phone.includes(searchTerm.phone)
                          );
                        } else if (searchTerm.name !== '' && searchTerm.department !== '') {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department)
                          );
                        } else if (searchTerm.name !== '') {
                          return student.userId.name.includes(searchTerm.name);
                        }
                      })

So above code, I am checking the search term object. By default searchTerm key values is empty string. Whenever user type something to related input, I catch that value and setting event.target.event to search term value. So instead of checking one by one for every object keys, I want this to happen just like below,


let returnValue ;
if( searchTerm.name !== ''){
returnValue += student.userId.name.includes(searchTerm.name)

}
if( searchTerm.department!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}

if( searchTerm.phone!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}

if( searchTerm.studentno!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}


return returnValue

I want it to happen like this.

The above code is wrong. I am aware of that. I am just asking that, since there is a way to do that with string, I mean we can combine the return values. But what if return values are not string type, how do we combine them. Is there any way to do that ? Or we just have to check one by one if else if else if … else and so on

What is the desired output? A string? A true/false? What?

That would be my guess too. So I’m confused as to why using = and && instead of += isn’t an acceptable solution?

Yes it is boolean. So;

let returnValue ;
if( searchTerm.name !== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)

}
if( searchTerm.department!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}

if( searchTerm.phone!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}

if( searchTerm.studentno!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}


return returnValue

Okay I think this should work ? No ?

I was actually really having brain overload when I started to ask the question.

Now I get the idea.

Thank you guys for sharing time for the question. Have a nice day !

@camperextraordinaire @JeremyLT

This was the what I was trying to achieve. And its final and working code …!

Now I am able to perform in memory search…

 {studentData
                      .filter(student => {
                        let returnValue = true;
                        if (searchTerm.name !== '') {
                          returnValue =
                            returnValue && student.userId.name.includes(searchTerm.name);
                        }
                        if (searchTerm.department !== '') {
                          returnValue =
                            returnValue &&
                            student.departmentId.name.includes(searchTerm.department);
                        }
                        if (searchTerm.phone !== '') {
                          returnValue =
                            returnValue && student.userId.phone.includes(searchTerm.phone);
                        }
                        if (searchTerm.studentno !== '') {
                          returnValue =
                            returnValue && student.studentno.includes(searchTerm.studentno);
                        }

                        return returnValue;
                      })

Thank you. The below code is searchTerm state.

  const [searchTerm, setSearchTerm] = useState({
    studentno: '',
    name: '',
    department: '',
    phone: ''
  });

The current logic is, if any value in search term is non empty, I compare it with the database value which is student property. If student property includes it then I return true, and eventually I return the related student with the search term. Search term may take multiple non empty values. Thats why I am combining them in returnValue.

Here is the actual result !

Yes. If any of the conditionals are not true ,then if returnValue false, then that student from the array will be excluded. So it will not be included to final incoming array.

That was exactly the desired behavior. I might have explained wrong above. Sorry for my english.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.