admin管理员组

文章数量:1410717

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

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

Share Improve this question asked Jul 11, 2014 at 0:45 Ahmad Seraj EddinAhmad Seraj Eddin 1711 silver badge8 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 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

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/guest271314/mU9T5/

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

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

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

  • hasOwnProperty : https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

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

Hope this fits your needs :)

code

array[i].hasOwnProperty('adress')

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

本文标签: jqueryjavascript check if value of object in array is not existStack Overflow