How to test if an array is defined, empty, or null?
I am retrieving JSON data via API from third party services. Below is my code:
- $.each(data.data, function(i, article) {
- var txtSum = article.summary.toString();
- var txtTitle = article.name;
- var re = new RegExp('\\b(' + searchValue + ')\\b', 'gi');
- txtSum = txtSum.replace(re, '<span>$1</span>');
- txtTitle = txtTitle.replace(re, '<span>$1</span>');
- output += '<h6>';
- output += '<a href="../kb/article/'+ article.url_hash + '">' + txtTitle + '</a>';
- if(typeof article.inherited_roles !== 'undefined' && article.inherited_roles > 0)
- {
- output += " <img src='../images/Lock-icon.png' />";
- }
- output += '</h6>';
- output += '<p>' + txtSum + '</p>';
- });
How do I check if inherited_roles is defined, empty, or null? I've tried to check the field inherited_roles in line #9 but it does not seem to work. The inherited_roles field's JSON value returned by the API is below:
- "inherited_roles": [
- "55e722df32131cf85008b580",
- "56a13f7c32131c7f6831a372",
- "56c4cfe091121c0b5b47fe66"
- ]
Some article has empty or null value. At this point every article shows up regardless if there are values in the inherited_roles field. Any suggestion is much appreciated.