Reputation: 171
I have this simple array with objects and values, but not all objects have same values:
var array = [
{name: 'aaa', mobile: '111-111'},
{name: 'bbb', mobile: '222-222', adress: '333-333'},
{name: 'ccc', mobile: '444-444', adress: '555-555'}
]
the first object doesn't have an address.
so when I tried to append it to html I got undefined
word:
jQuery:
$('ul').append(
'<li>'+array[i].name+'</li>'+
'<li>'+array[i].mobile+'</li>'+
'<li>'+array[i].adress+'</li>'+
)}
HTML:
<li>aaa</li>
<li>111-111</li>
<li>undefined</li>
<li>bbb</li>
<li>222-222</li>
<li>333-333</li>
how can to check if this value not exist then show nothing instead of undefined
word?, or even to not show the whole li
tag?
Upvotes: 1
Views: 1396
Reputation: 2716
JavaScript has an inherent Boolean value, generally known as either truthy or falsy. A unassigned value will return false in an if statement ie
if(array[i].adress){
I added a shorthand if statement to your code to fix the problem.
function details () {
$('ul').append(
'<li>'+array[i].name+'</li>'+
'<li>'+array[i].mobile+'</li>'+
(array[i].adress? '<li>'+ array[i].adress +'</li>': '')
);
}
Upvotes: 2
Reputation: 2014
check this one
if (array[i].adress==null)
{
array[i]['adress'] = 'None';
}
$('ul').append(
'<li>'+array[i].name+'</li>'+
'<li>'+array[i].mobile+'</li>'+
'<li>'+array[i].adress+'</li>'+
'<br />'
);
Upvotes: 0
Reputation: 1
Try
var array = [
{name: 'aaa', mobile: '111-111'},
{name: 'bbb', mobile: '222-222', adress: '333-333'},
{name: 'ccc', mobile: '444-444', adress: '555-555'}
];
$.each(array, function(index, value) {
$.each(value, function(key, val) {
$("<li>", {
"text": val
}).appendTo("ul")
});
});
jsfiddle http://jsfiddle.net/guest271314/mU9T5/
Upvotes: 3
Reputation: 34107
Waht you are looking for is this: http://jsfiddle.net/Z7U3r/
hasOwnProperty
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnPropertyThe hasOwnProperty() method returns a boolean indicating whether the object has the specified property.
Hope this fits your needs :)
code
array[i].hasOwnProperty('adress')
Upvotes: 1
Reputation: 13334
You can use something like this:
function details () {
$('ul').append(
'<li>'+array[i].name+'</li>'+
'<li>'+array[i].mobile+'</li>'+
(typeof(array[i].adress) === 'undefined' ? '' :
('<li>'+array[i].adress+'</li>')) +
'<br />'
);
}
I used ternary if
with the condition that checked whether address
property was defined. If not, the entire <li>
is omitted.
Upvotes: 1
Reputation: 2831
you need to set it's adress still but with ' '
value.
var array = [
{name: 'aaa', mobile: '111-111',adress:''},
{name: 'bbb', mobile: '222-222', adress: '333-333'},
{name: 'ccc', mobile: '444-444', adress: '555-555'}
]
EDIT :
SOLUTION 2
check if array[i].adress
is typeof
Undefined
function details () {
if( typeof array[i].adress == "undefined"){
array[i].adress = '';
}
$('ul').append(
'<li>'+array[i].name+'</li>'+
'<li>'+array[i].mobile+'</li>'+
'<li>'+array[i].adress+'</li>'+
'<br />'
);
}
Fiddle updated : Working Demo
Upvotes: 3