jQuery .each() 数组中的 .each() 数组
jQuery .each() array in .each() array
听到对象阵列
advancedParams = [];
advancedParams["cost"] = [];
advancedParams["cost"]["from"] = 10;
advancedParams["cost"]["to"] = 100;
但是 .each() 看不到第二个数组
$.each(advancedParams, function( idx, obj ) {
console.log(idx); // shows cost
console.log(obj ); // shows [from: "10", to: "200"]
$.each(obj, function( key, value ) {
console.log(key); // nothing
//each have no iterations, why?
});
}
这是绝对无效的:
{cost: [from: "10"], [to: "200"]}
因为在数组中你不能用 :
分隔值。
您应该分配一个对象,而不是声明一个数组 []
。
advancedParams = {};
advancedParams["cost"] = {};
查看下面的演示。
var advancedParams = {};
advancedParams["cost"] = {};
advancedParams["cost"]["from"] = 10;
advancedParams["cost"]["to"] = 100;
$.each(advancedParams, function(i, obj) {
console.log(obj); // results in "Object {from: 10, to: 100}"
$.each(obj, function(i, item) {
console.log(item); // results in "10, 100"
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您的对象定义无效。查看下面的错误消息:
将其存储在这样的对象中:var obj = { from: 10, to:200}
。或者如果你想要一个对象数组,这样做:var arr =[{from:10,to:200},{from:1, to:300}]
.
听到对象阵列
advancedParams = [];
advancedParams["cost"] = [];
advancedParams["cost"]["from"] = 10;
advancedParams["cost"]["to"] = 100;
但是 .each() 看不到第二个数组
$.each(advancedParams, function( idx, obj ) {
console.log(idx); // shows cost
console.log(obj ); // shows [from: "10", to: "200"]
$.each(obj, function( key, value ) {
console.log(key); // nothing
//each have no iterations, why?
});
}
这是绝对无效的:
{cost: [from: "10"], [to: "200"]}
因为在数组中你不能用 :
分隔值。
您应该分配一个对象,而不是声明一个数组 []
。
advancedParams = {};
advancedParams["cost"] = {};
查看下面的演示。
var advancedParams = {};
advancedParams["cost"] = {};
advancedParams["cost"]["from"] = 10;
advancedParams["cost"]["to"] = 100;
$.each(advancedParams, function(i, obj) {
console.log(obj); // results in "Object {from: 10, to: 100}"
$.each(obj, function(i, item) {
console.log(item); // results in "10, 100"
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您的对象定义无效。查看下面的错误消息:
将其存储在这样的对象中:var obj = { from: 10, to:200}
。或者如果你想要一个对象数组,这样做:var arr =[{from:10,to:200},{from:1, to:300}]
.