我有这个jquery数组
我正在尝试迭代它,但我总是得到第一个值[false,false]并且循环结束
我正在使用此函数进行迭代
C:\Users\ogautier\RoR\holamundo>rails server
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10-x64-mingw32/lib/sqlite3.rb
:6:in `require': cannot load such file -- sqlite3/sqlite3_native (LoadError)
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10-x64-mingw32/l
ib/sqlite3.rb:6:in `rescue in <top (required)>'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10-x64-mingw32/l
ib/sqlite3.rb:2:in `<top (required)>'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:76:in `require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:76:in `block (2 levels) in require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:72:in `each'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:72:in `block in require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:61:in `each'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler/ru
ntime.rb:61:in `require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.9.7/lib/bundler.rb
:134:in `require'
from C:/Users/ogautier/RoR/holamundo/config/application.rb:7:in `<top (r
equired)>'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:78:in `require'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:78:in `block in server'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:75:in `tap'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:75:in `server'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands/commands_tasks.rb:39:in `run_command!'
from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/com
mands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
没有成功,任何帮助都将不胜感激
Jsfiddle - &gt; https://jsfiddle.net/xvcg0spz/
答案 0 :(得分:0)
您将url_array声明为数组
var url_array = [];
但未将其用作一个。数组采用数字索引,javascript没有关联数组(将字符串作为索引的数组)。
执行以下操作:
url_array['/'] = [false, false];
url_array['/cart'] = [true, false];
url_array['/checkout/34'] = [true, false];
您正在设置数组对象的属性,而不是向数组添加任何内容。所以你的数组是空的。由于它是一个数组实例,jQuery会将其视为这样,并尝试访问数组元素而不是您设置的属性。
简单地将url_array声明为对象,jQuery将正确迭代属性
var url_array = {}; //you should probably rename it since its not really an array
<强>演示强>
var url_array = {};
url_array['/'] = [false, false];
url_array['/cart'] = [true, false];
url_array['/checkout/34'] = [true, false];
url_array['/user/register'] = [true, true];
url_array['/checkout/34/checkout'] = [true, false];
url_array['/checkout/34/review'] = [true, false];
url_array['/product-categories/aaaa11'] = [false, false];
url_array['/product/54/38'] = [false,false];
url_array['/our-stores'] = [true,false];
url_array['/user'] = [true,true];
url_array['/news'] = [false,false];
url_array['/Shows'] = [false,false];
url_array['/content/Biography'] = [false,false];
function CompareAndShowOrHide(url) {
$.each(url_array , function( index, obj ) {
$.each(obj, function( key, value ) {
console.log(key);
console.log(value);
});
});
}
CompareAndShowOrHide();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
答案 1 :(得分:0)
基本上,问题是jQuery.each将url_array视为一个数组,因为这就是你定义它的方式。但它将它视为一个长度为0的数组,所以它永远不会烦恼迭代任何东西。
您存储在其中的值与数组的属性相关联 - 它们不是您想到它们的数组元素。您可能希望在JavaScript中查看关联数组。
但是,有一个简单的解决方案。你只需要将你的“数组”定义为它的实际内容:一个对象。
var url_array = {};
然后使用jQuery.each的对象版本迭代它们:
$.each(url_array, function(key, value) {
});
请参阅this fiddle。