I have the following code
var oDataEn = aData[0][aProperties[0].split('/')[0]]; if (!(oDataEn != null && oDataEn.results && oDataEn.results.length > 0)) { ... else ...
This works OK , unless
aData[0] = 'undefined'
my question is is there a better way to write and not add up
if(aData[0] != null) { var oDataEn = aData[0][aProperties[0].split('/')[0]]; }
I do not want to have two, if possible ...
Start with a ternary point - I assume that aData [0] could be false (null, undefined, 0 or ""):
var oDataEn = aData[0]?aData[0][aProperties[0].split('/')[0]]:null; if (oDataEn && oDataEn.results && oDataEn.results.length > 0) {
if(aData[0] === undefined){}
is the best way, as far as I know.
According to your comment
if(typeof aData[0] != 'undefined' /*&& other if statements*/){}
Therefore, it will not be processed if it is undefined.
Using the ternary operator u, we can do this. The ternary operator will reduce only the line of codes.
var oDataEn = aData[0] === undefined ? '' : aData[0][aProperties[0].split('/')[0]];
Source: https://habr.com/ru/post/1246045/More articles:Problem connecting to SQL Server tables with the same plugin and the same data type - sql-serverJavascript convert blob to string and vice versa - javascriptThe reset dropdown value for the first value when the condition is met - javascriptASP.NET Kernel - Problems Debugging Through Docker in Visual Studio 2015 - c #Is sys.exit equivalent to raising SystemExit? - pythonHow to securely exchange data between two or more applications in Android? - androidSpark SQL package not found - javaHow to connect to FTP through a TLS / SSL (FTPS) server in Java - javaWhat is the correct way to insert activity into another project? - androidHow to insert translated text into php database - javascriptAll Articles