1

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?

JSFiddle DEMO

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have hundreds of elements and about 15 values for each element, so i can't change them manually
3

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/

2 Comments

Yes, this is much elegant in some aspect. +1
Great solution. +1 (Added in the break between li)
2

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>': '')
);
}

2 Comments

Your if is better than mine. I forgot that undefined is falthy.
Thnk you @Bryan, that's what I want :)
1

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.

SQL Fiddle

Comments

1

Waht you are looking for is this: http://jsfiddle.net/Z7U3r/

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property.

Hope this fits your needs :)

code

array[i].hasOwnProperty('adress')

1 Comment

@AhmadSeraj yep, thats why this hasOwnProperty method exist. no need to do raw undefined cheacks when the method does it for you. and in optimality it might be faster :) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
0

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 />'
);

2 Comments

in this way all the values of an element that doesn't have address value will disappear , and that not what I want
Try this one simillary you can add else if for name and mobile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.