Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

If otherwise, compare the table to the string

$
0
0

I have the following if-else :

if (entity.length > depot.length) { for (var i = 0 ; i < entity.length; i++) { promises.push(this.getDataFromREST(security, i)); console.log(entity, depot) } } else { for (var i = 0 ; i < depot.length; i++) { promises.push(this.getDataFromREST(security, i)); console.log(entity, depot) } }

Where entity and depot are arrays, but if they are not holding data they = "NULL" .

Basically,for the purposes of this explanation, I want the length of "NULL" to be read as 0, or if entity is an array of length 1+ and depot is a string - the condition entity.length > depot.length is met.

If both entity and depot can be either "NULL" string or a valid array, you can simply make a check for "NULL" , calculate its length once and use one loop:

var loopLength = Math.max(entity === 'NULL' ? 0 : entity.length, depot === 'NULL' ? 0 : depot.length); for (var i = 0 ; i < loopLength; i++) { promises.push(this.getDataFromREST(security, i)); console.log(entity, depot) }

This loop will run only if at least entity or depot is not "NULL" string and a valid non-empty array.


Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles





Latest Images