javascript函数返回的数组不工作
我有一个函数,它在子进程中处理一些数组 目标是将实际数组发送到函数中,并返回更新的数组(删除2个值) 我通过以下方式调用该函数:javascript函数返回的数组不工作,javascript,arrays,Javascript,Arrays,我有一个函数,它在子进程中处理一些数组 目标是将实际数组发送到函数中,并返回更新的数组(删除2个值) 我通过以下方式调用该函数: var resultMaster = []; resultMaster = traceSystem(traceURL,searchURL,term,master_id,groupID,final_pair,masters); console.log('resultMaster: ' + resultMaster); console.log('re
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;
};
来自循环的日志:
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
在循环之后立即记录:
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>
我看不出通过其他方式所做的有什么不同。除了现在仅使用一个参数调用子函数之外。报告的问题似乎不是问题 我刚刚重新格式化了代码中的行,它开始工作了
可能是一个隐藏字符逃过了实际返回值 报告的问题似乎不是问题 我刚刚重新格式化了代码中的行,它开始工作了 可能是一个隐藏字符逃过了实际返回值 您不能这样做,因为$.ajax()是(而且应该是)一个异步函数 发生的情况是,traceSystem()函数在实际调用成功回调之前返回 因此,在实际修改对象之前,您将返回
masters
对象
在服务器端,您可以使用deasync(nodejs模块),但在浏览器中,我认为最好的办法是重写代码,以异步方式执行
例如:
traceSystem()
函数中[...]
traceSystem(arg1, arg2... , function(resultMaster){
console.log('resultMaster: ' + resultMaster);
[...]
});
不能这样做,因为$.ajax()是(而且应该是)一个异步函数
发生的情况是,traceSystem()函数在实际调用成功回调之前返回
因此,在实际修改对象之前,您将返回masters
对象
在服务器端,您可以使用deasync(nodejs模块),但在浏览器中,我认为最好的办法是重写代码,以异步方式执行
例如:
traceSystem()
函数中[...]
traceSystem(arg1, arg2... , function(resultMaster){
console.log('resultMaster: ' + resultMaster);
[...]
});
代码显示
async:false,
-这不是异步调用。这没有意义。您需要记录一个值。返回该值。记录返回结果。他们是不同的。我想不出在这种情况下会发生什么。你能使你的代码更简洁、更完整吗?值得注意的是<代码>异步:false ,并且你应该考虑浏览器可以在任何时候开始错误地抛出错误,打断你的代码。@昆廷,我会尝试…给我一个timeso这意味着james thorpe是正确的?代码说async:false,
-这不是一个异步调用。这没有意义。您需要记录一个值。返回该值。记录返回结果。他们是不同的。我想不出在这种情况下会发生什么。你能使你的代码更简洁、更完整吗?值得注意的是<代码>异步:false ,并且你应该考虑浏览器可以在任何时候开始错误地抛出错误,打断你的代码。@昆廷,我会尝试…给我一个timeso这意味着james thorpe是对的?我不这么认为:这应该是延迟的副作用(就像执行警报(“Foo bar”)
调用函数后,给ajax处理留出完成的时间。在其他浏览器或随机的其他情况下,它会失败……我不这么认为:这应该是延迟的副作用(就像执行警报(“Foo bar”)
在调用函数后,给ajax处理留出完成的时间。在其他浏览器或其他随机情况下,它会失败。。。