2012-03-03 13 views
0

私はそれにいくつかの変数を持っている配列をチェックする方法を知りたいと思います。 次のコードを使って説明してください:配列内のチェック方法..配列内で? jquery

// Ignore the words in here, they are just there to explan. 
var $array = [ 
    alpha = { 
    name : $('.a'), 
    date : $('.b'), 
    street: $('.c') 
    }, 
    beta = { 
    name : $('.x'), 
    date : $('.y'), 
    street: $('.z') 
    }  
]; 

/* So now I got this arrays.. withing an array? is that correct? 
* or what is this called? 
* and now I want to check each object's length, if its 0 -> return false and 
* throw out an error, if >= 1 go on and check the next. */ 

// Check if all needed elements for the toggle to work are existent 
$.each($array, function(key, value) { 
    if (!value.length) { 
    alert("Could not find "+ '"' + value.selector +'"!') 
    return false 
    }; 
}); 
// Well obviously this doesnt work.. 

ありがとうございます!

+4

あなたはおそらく研究を行うと**オブジェクトを知っている必要があります。**から**配列** – Joseph

答えて

1

することはできfor (... in ...)ループとobjectのプロパティの名前をループ:

/* since the array isn't a jQuery object, don't prefix the name with `$` */ 
var things = [ 
    /* array elements have integer, not string, indices. In any case, 
     "alpha = ..." is the wrong syntax to set properties. What it 
     does is set a variable named "alpha". 
    */ 
    {...}, 
    {...}, 
]; 

$.each(things, function(idx, item) { 
    for (p in item) { 
    if (! item[p].length) { 
     /* alerts are disruptive; use other means of informing 
     the user or developer. 
     */ 
     //alert("Could not find "+ '"' + value.selector +'"!') 
     return false; 
    } /* blocks don't need semicolon separators */ 
    } 
}); 

も参照してください「For .. in loop?

+0

ここに(p in item)は何ですか? –

+0

@MartinBroder:それはリンクのためのものです。 – outis

+0

おそらくそのループにhasOwnPropertyを使うべきでしょう:https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Object/HasOwnProperty –