Skip to content Skip to sidebar Skip to footer

Javascript Using Or Operator To Iterate Over List Of Strings

I'm having trouble with the following piece of code: I expected only the values '3','4','1', and '2' would be displayed, but instead they are all being displayed and I don't under

Solution 1:

Unfortunately, that's not how the || operator works. You'd have to write:

fileheader[j] !== 'GENE_NAME' || fileheader[j] !== 'logCPM'// ...etc

The other option is creating an array and using indexOf:

if (['GENE_NAME', 'logCPM', 'logFC', 'FOR', 'PValue'].indexOf(j) < 0) {
}

If you're only worried about newer browsers, you may also be able to get away with includes instead of indexOf.

Solution 2:

Try to write your code like,

var fileheader = ["GENE_NAME", "3", "4", "1", "2", "logFC", "logCPM", "PValue", "FDR"];
var filter = ['GENE_NAME', 'logCPM', 'logFC', 'FDR', 'PValue'];
for (var j=0; j<fileheader.length; j++){
    if (!filter.includes(fileheader[j])){
        console.log(fileheader[j])
    }
}

Note about using Array.prototype.includes(),

It is a new installment to ES6, and it has limited browser support. It don't have support with IE even in recent versions. As an alternate to it you could use Array.prototype.indexOf(). Using it will give you the index of matched element, if nothing matches then it will return -1. Based on that you could modify your code.

Now lets come to the problem present in your code,

Have a look at the snippet below,

('GENE_NAME'||'logCPM'||'logFC'||'FDR'||'PValue')

Would always be evaluated to 'GENE_NAME'. So your code is very much similar to,

var fileheader = ["GENE_NAME", "3", "4", "1", "2", "logFC", "logCPM", "PValue", "FDR"]
for (var j=0; j<fileheader.length; j++){
    if (fileheader[j] !== 'GENE_NAME'){
        console.log(fileheader[j])
    }
}

Solution 3:

If your goal is to show only Numbers you can use isNaN:

var fileheader = ["GENE_NAME", "3", "4", "1", "2", "logFC", "logCPM", "PValue", "FDR"]
for (var j=0; j<fileheader.length; j++){
    if (!isNaN(fileheader[j])){
        console.log(fileheader[j])
    }
}

Solution 4:

Usually || is used to evaluate a sequence of conditions, expressions that evaluate to true/false, to some value or no value, or similar scenarios.

You can do cool things, if you use this to control the sequence of execution in eachs expressions return value, as it stops after the firstevaluation to true.

It is called logical or, however it returns something different in JavaScript (EcmaScript). Apparently the first String in your sequence. And the rest is obvious enough. All except the first are inequal an show up.

Here is the explanation:

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. The && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.

Returns expr1 if it can be converted to true; otherwise, returns expr2.

https://developer.mozilla.org/de/docs/Web/JavaScript/Guide/Ausdruecke_und_Operatoren#Logical_operators

Post a Comment for "Javascript Using Or Operator To Iterate Over List Of Strings"