Javascript 如何检查数组。。在一个数组中?jquery
我想知道如何检查包含多个变量的数组。。差不多吧。 我可以用下面的代码更好地解释:Javascript 如何检查数组。。在一个数组中?jquery,javascript,jquery,arrays,Javascript,Jquery,Arrays,我想知道如何检查包含多个变量的数组。。差不多吧。 我可以用下面的代码更好地解释: // 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'), st
// 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..
提前谢谢 您可以使用一个循环来循环属性的名称:
另请参见“”您可能应该进行研究,并从阵列中了解一个对象此处的(项目中的p)是什么?@MartinBroder:这就是链接的用途。您可能还应该在该循环中使用hasOwnProperty:
/* 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 */
}
});