jquery.each 多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3557818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery.each multidimensional array
提问by dazz
var array1 = {};
array1['one'] = new Array();
array1['one']['data'] = 'some text';
array1['one']['two'] = new Array();
array1['one']['two']['three'] = new Array();
array1['one']['two']['three']['data'] = 'some other text';
$.each(array1, function(key1, value1){
$.each(value1['two']['three'], function(key1, value1){
document.write('test');
}
});
everything works, except it doesnt get to the document.write. Anyone an idea why?
一切正常,除了它没有到达 document.write。有人知道为什么吗?
采纳答案by Mark Schultheiss
Note that the Array() replacement is key here with the missing ')'
请注意, Array() 替换是此处缺少的 ')' 的关键
var array1 = {};
array1['one'] = new Object();
array1['one']['data'] = 'some text';
array1['one']['two'] = new Object();
array1['one']['two']['three'] = new Object();
array1['one']['two']['three']['data'] = 'some other text';
$.each(array1, function(key1, value1) {
$.each(value1['two']['three'], function(key1, value1) {
document.write('test');
});
});
and Another way to write the same thing:(small tweek on the write to reference your object)
和另一种写同样的东西的方法:(写上的小tweek来引用你的对象)
var array1 = {};
array1.one = new Object();
array1.one.data = 'some text';
array1.one.two = new Object();
array1.one.two.three = new Object();
array1.one.two.three.data = 'some other text';
$.each(array1, function(key1, value1) {
$.each(value1['two']['three'], function(key1, value1) {
document.write('test' + array1.one.data);
});
});
And finally, with the deprecated new Object() replacement:
最后,使用已弃用的 new Object() 替换:
var array1 = {};
array1['one'] = {}
array1['one']['data'] = 'some text';
array1['one']['two'] = {};
array1['one']['two']['three'] = {};
array1['one']['two']['three']['data'] = 'some other text';
$.each(array1, function(key1, value1) {
$.each(value1['two']['three'], function(key1, value1) {
document.write('test');
});
});
EDIT: some fun with your array, and why you MIGHT have the strings in the object declaration as you have it:
编辑:你的数组很有趣,以及为什么你可能在对象声明中包含字符串,因为你拥有它:
var array1 = {};
var fun="four";
array1.one = {};
array1.one.data = 'some text';
array1.one.two = {};
array1.one.two.three = {};
array1.one.two.three.data = 'some other text';
array1.one.two[fun] = {};
array1.one.two[fun].data=' howdy';
$.each(array1, function(key1, value1) {
$.each(value1.two.three, function(key1, value1) {
document.write('test'+array1.one.two[fun].data+ ":"+key1+":"+value1);
});
});
the output the the last is: "test howdy:data:some other text"
最后的输出是:“test howdy:data:some other text”
回答by ConroyP
The document.write isn't working as you've a syntax error, so the code flow never gets to it - you need another bracket at the end of your each
, i.e.
document.write 不工作,因为你有一个语法错误,所以代码流永远不会得到它 - 你需要在你的末尾另一个括号each
,即
$.each(array1, function(key1, value1){
$.each(value1['two']['three'], function(key1, value1){
document.write('test');
})
});
If you're going to be doing any non-trivial work with javascript, I'd highly recommend that you use Firefox with Firebuginstalled - it's console highlights these kind of errors which would otherwise fail without you realising, leading you to believe everything was working ok.
如果您打算使用 javascript 进行任何非平凡的工作,我强烈建议您使用安装了Firebug 的Firefox - 它的控制台突出显示了这些错误,否则这些错误将在您没有意识到的情况下失败,让您相信一切都是工作正常。
回答by Topera
You miss )
in the second each
.
你错过)
了第二个each
。