我有一个功能,可以在子过程中处理一些数组。
目标是将实际数组发送到函数中,并返回更新后的数组(删除了2个值)。
我通过以下方式调用该函数:
var resultMaster = [];
resultMaster = traceSystem(traceURL,searchURL,term,master_id,groupID,final_pair,masters);
console.log('resultMaster: ' + resultMaster);
console.log('resultSystem under Master: ' + masters);
所调用的函数执行以下操作:
function traceSystem(traceURL,searchURL,term,master_id,groupID,pairKey,masters) {
var resultSystem = [];
...
$.ajax({
url: searchURL,
type:'post',
data: jsonQuery,
dataType: 'text',
crossDomain: true,
async: false,
success: function(response) {
...
for ( m = 0; m < system.masters_ids.buckets.length; m++ ) {
console.log('Removing masterID: ' + system.masters_ids.buckets[m].key);
masters = masters.filter(function(e) { return e != system.masters_ids.buckets[m].key });
console.log('resultSystem: ' + masters);
};
} // Success
}); //Ajax
console.log('final result from System: ' + masters);
return masters;
};
我以为return masters;
会将数组返回父函数到变量resultMaster中。但事实并非如此。
从控制台查看日志:
Array [ "ci1481537045764.949410@czcholsint37…", "ci1481537045768.924200@czcholsint37…", "3b3_87465652", "00000553239291", "ci1481536983712.948609@czcholsint37…", "ci1481536983718.923358@czcholsint37…" ]
循环日志:
Removing masterID: 3b3_87465652
resultSystem: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_te
Removing masterID: ci1481537045764.949410@czcholsint373_te
resultSystem: ci1481537045768.924200@czcholsint372_te,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_te
循环后立即登录:
final result from System: ci1481537045768.924200@czcholsint372_te,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_teindex-by-masterid.jsp:343:5
从traceSystem返回值后,从父函数记录日志:
resultMaster:
resultSystem under Master: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,3b3_87465652,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_te
可变的resultMasters似乎是空的。
如何正确地从调用函数返回数组?
根据@Quentin请求添加:
我试图创建一个单独的简单示例来模拟行为,但奇怪的是它正在起作用:
<script type="text/javascript">
function traceMasterId(masters) {
var resultMaster = [],
masters = [];
masters.push('ci1481537045764.949410@czcholsint373_te');
masters.push('ci1481537045768.924200@czcholsint372_te');
masters.push('ci1481536983712.948609@czcholsint373_te');
masters.push('ci1481536983718.923358@czcholsint372_te');
masters.push('3b3_87465652');
masters.push('00000553239291');
console.log('Masters: ' + masters);
resultMaster = traceSystem(masters);
console.log('resultMaster: ' + resultMaster);
};
function traceSystem(masters) {
var master_ids = [];
master_ids.push('ci1481536983718.923358@czcholsint372_te');
master_ids.push('3b3_87465652');
for ( m = 0; m < master_ids.length; m++ ) {
console.log('Removing masterID: ' + master_ids[m]);
masters = masters.filter(function(e) { return e != master_ids[m] });
console.log('resultSystem: ' + masters);
};
console.log('Final result from System: ' + masters);
return masters;
};
</script>
<body onload="traceMasterId();"></body>
查看日志:
Masters: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_te,3b3_87465652,00000553239291test.jsp:14:5
Removing masterID: ci1481536983718.923358@czcholsint372_tetest.jsp:29:6
resultSystem: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,ci1481536983712.948609@czcholsint373_te,3b3_87465652,00000553239291test.jsp:31:6
Removing masterID: 3b3_87465652test.jsp:29:6
resultSystem: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,ci1481536983712.948609@czcholsint373_te,00000553239291test.jsp:31:6
Final result from System: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,ci1481536983712.948609@czcholsint373_te,00000553239291test.jsp:34:5
resultMaster: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,ci1481536983712.948609@czcholsint373_te,00000553239291test.jsp:18:5
我看不出其他方法有什么不同。现在只用一个参数调用了子功能。
您不能因为$ .ajax()是(并且应该是)异步函数。
发生的情况是,在实际调用成功回调之前,traceSystem()函数将返回。
因此,您需要masters
在实际修改之前返回对象。
在服务器端,您可能曾经使用过deasync(nodejs模块),但是在浏览器中,我认为最好的办法是重写代码以异步方式进行操作。
也就是说,例如:
向函数添加回调参数traceSystem()
。
在$ .ajax()成功回调的末尾调用它(将master作为参数传递)。
代替...
这:
[...]
resultMaster = traceSystem(arg1, arg2...);
console.log('resultMaster: ' + resultMaster);
[...]
...尝试:
[...]
traceSystem(arg1, arg2... , function(resultMaster){
console.log('resultMaster: ' + resultMaster);
[...]
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句