JavaScript 数组对象的问题
Issue with javascript array object
我有以下JSON响应。我正在使用 $.getJSON 方法来加载 JSON 数据,并使用回调函数通过检查它是否是数组来执行一些操作,如下所示。
{
"r": [{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg": [{
"Name": "james",
"Rate": 0.05
}, {
"Name": "Jack",
"Rate": 0.55
}, {
"Name": "Mcd",
"Rate": 0.01,
}],
},
{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg": [{
"Name": "james",
"Rate": 0.05
}, {
"Name": "Jack",
"Rate": 0.55
}, {
"Name": "Mcd",
"Rate": 0.01,
}],
},
{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg": [{
"Name": "james",
"Rate": 0.05
}, {
"Name": "Jack",
"Rate": 0.55
}, {
"Name": "Mcd",
"Rate": 0.01,
}],
}]
}
我正在将 loadFromJson1 和 loadFromJson2 函数上的 json 响应作为"输入"作为参数传递,如下所示。
var tablesResult = loadFromJson1(resultstest.r[0].Clg);
loadFromJson1 = function (input) {
if (_.isArray(input)) {
alert("loadFromJson1: Inside array function");
var collection = new CompeCollection();
_.each(input, function (modelData) {
collection.add(loadFromJson1(modelData));
});
return collection;
}
return new CompeModel({
compeRates: loadFromJson2(input),
compName: input.Name
});
};
loadFromJson2 = function (input)
// here is the problem, the 'input' is not an array object so it is not going to IF condition of the isArray method.
{
if (_.isArray(input)) {
alert("loadFromJson2: Inside array function");
//alert is not coming here though it is an array
var rcollect = new rateCollection();
_.each(input, function (modelData) {
rcollect.add(modelData);
});
return rcollect;
}
};
上面的代码我正在传递 loadFromJson1 和 loadFromJson2 的 json 响应作为"输入"。 isArray 仅在 loadFromJson1 函数上变为真,并在 if 条件内发出警报,但没有进入 loadFromJson2 函数,尽管我传递了相同的参数。
谁能告诉我为什么 loadFromJson2 函数在我传递数组对象时没有收到内部警报?
如果input
是一个数组,则不要调用loadFromJson2
。 仅当input
不是数组时才调用它。 所以_.isArray(input)
永远不会在loadFromJson2
里面是真的.
这里有一个 jsfiddle 演示了它(打开浏览器的 javascript 控制台以查看日志)。 您可以从输入中获得此输出:
into loadFromJson1 call #1 (index):82
loadFromJson1 #1: it's an Array (index):84
loadFromJson1 #1: Inside array function (index):85
into loadFromJson1 call #2 (index):82
loadFromJson1 #2: not an array (index):93
loadFromJson1 #2: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
into loadFromJson1 call #3 (index):82
loadFromJson1 #3: not an array (index):93
loadFromJson1 #3: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
into loadFromJson1 call #4 (index):82
loadFromJson1 #4: not an array (index):93
loadFromJson1 #4: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
loadFromJson1 #1: returning (index):90
如您所见,对loadFromJson2
的调用来自对loadFromJson1
的嵌套调用 - 那些获取不是 Array 的内容的调用。
相关文章:
- JavaScript代码问题:我正在将对象转换为数组
- jquery$(this)对象缺少问题
- JavaScript对象继承问题
- 在对象中添加方法时出现问题
- 基本对象/原型语法问题
- Javascript对象文字,POST问题
- 使用Javascript对象为具有两个背景图像的DIV设置样式时出现问题
- 使用.map解析JSON对象时出现问题
- 类中对象的奇怪问题(John Resig简单继承)
- 访问Javascript对象-Node.js的作用域问题
- JavaScript对象中存在数组的问题
- 递归Javascript对象是否会导致任何问题(内存泄漏)
- JavaScript—父类和子类/对象的此问题
- 我该如何解决这个问题;参考网格”;在JavaScript中完成的对象数
- jQuery拖动到相对位置的可排序列表中时出现可拖动辅助对象位置问题
- 尝试将变量传递到对象中时的作用域问题'的方法构造函数
- 创建jQuery对象时出现问题
- 面向对象,控制器问题,初学者Javascript
- 从 JSON 对象获取数据时出现问题
- Rails的问题,haml + javascript在字段对象的模糊