How to assign Logical Or operator in varaible [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to assign || operator to variable.
var status;
if(this.state.selectedStatus === 'ALL'){
status = 'Working' || 'Left' #I need this how can we get this
}else{
status = this.state.selectedStatus
}
#It gives only 'working' output. But I need 'Working' || 'Left'
// For this
if(this.state.allEmployees[i].Status === status){
gEmployees.push({
value: this.state.allEmployees[i].Name,
id: this.state.allEmployees[i].EmployeeId
})
}

If you want to store an operator for use later, then it needs to be part of a function (and it is the function you store).
To solve this problem, however, just use an array:
status = ["Working", "Left"];
and make use of the includes method to find out if the Status matches one of the values in it.
if ( status.includes(this.state.allEmployees[i].Status) ) {

Related

filter an array by property returns unfiltered array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to filter an array of objects by a property like this
if (payment == Payment.CREDIT_CARD) {
this.currenies.filter((currency: Currency) => currency.isFromEurope === true);
console.log(this.currencies)
}
Initially, currencies array has 135 items, and after filtering the same number of items (at least 30 of them have isFromEurope = true);
From the documentation of Array.filter:
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
i.e. it does not change the original array.
Plus, you have a typo too (this.currenies).
It should be like this:
if (payment == Payment.CREDIT_CARD) {
this.currencies = this.currencies.filter((currency: Currency) => currency.isFromEurope === true);
console.log(this.currencies)
}

Where to store return messages for javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Where should I store message types javascript function is returning? I can just return string messages, but it seems like a bad idea. Are there any best practices?
For example in this kind of function (of course real functions make more sense then this)
function isFooValid(foo){
if(foo.length < 4){
return "TOO_SHORT";
} else if(foo.length >100) {
return "TOO_LONG";
} else if(foo.charAt(1) == 'A'){
return "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A";
}
}
You can create a variable for that. For example you can have:
myMessages ={
"secondChar": "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A",
"tooShort": "TOO_SHORT"
}
Then you can retrieve them as myMessages.tooShort, for example.
Even, if you want to have something similar to the string resources in android, you can store all the strings in a json file. If you load it before you js script, you are going to have the myMessages var available.

Find an element that has a text value of 0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to find an element that's text value is 0.
I do:
var d = $('.content').find('div');
if(d.text().length === 0){
//do something
}
Is there a way to put the above logic in the selector?
I've tried:
var d = $('.content').find('div:empty');
But no luck as the div may have other empty html tags in.
Use a filter
var elems = $('.content').find('div').filter(function() { return $(this).text() == 0 });

Can someone explain to me why arguments within functions immediately get assigned the value of the object at hand? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the code below:
// Get Index of object based on id value
var arrayPosition = userListData.map(function (arrayItem) {
return arrayItem.username;
}).indexOf(thisUserName);
Why is it that arrayItem = userListData[0], userListData[1], userListData[2]... ?
In general, they do not.
That is just what the map function is designed to do.
callback —
Function that produces an element of the new Array, taking three arguments:
currentValue — The current element being processed in the array.
etc

Comparing URLS in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Whats another way to compare URLs? This is not working. Its been a really long while since i've coded
var myURL =
(window.location.href="http://randbox.blogspot.com/2014/01/test-0.html");
if ( alert(document.URL) == myURL ) {
var myURL =
(window.location.href="http://randbox.blogspot.com/2014/01/test-0.html");
if ( document.URL == myURL ) {
Remove the alert() function. It's not comparable to a variable, so it will return false.
If it still doesn't work, document.URL might not be what you're wanting, or the string is incorrect, or maybe both are wrong.

Categories