I have a function which process some array in sub-process.
The goal is to send the actual array into function and return the array updated (2 values removed).
I am calling the function by:
var resultMaster = [];
resultMaster = traceSystem(traceURL,searchURL,term,master_id,groupID,final_pair,masters);
console.log('resultMaster: ' + resultMaster);
console.log('resultSystem under Master: ' + masters);
the called function does:
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;
};
I supposed that the return masters;
will return the array to parent function into the variable resultMaster. But it doesn't.
See log from console:
Array [ "ci1481537045764.949410@czcholsint37…", "ci1481537045768.924200@czcholsint37…", "3b3_87465652", "00000553239291", "ci1481536983712.948609@czcholsint37…", "ci1481536983718.923358@czcholsint37…" ]
Logs from the loop:
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
Log right after the loop:
final result from System: ci1481537045768.924200@czcholsint372_te,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_teindex-by-masterid.jsp:343:5
Log from parent function after returned value from traceSystem:
resultMaster:
resultSystem under Master: ci1481537045764.949410@czcholsint373_te,ci1481537045768.924200@czcholsint372_te,3b3_87465652,00000553239291,ci1481536983712.948609@czcholsint373_te,ci1481536983718.923358@czcholsint372_te
Variable resultMasters seems to be empty.
How to properly return the array from called function?
Added based on @Quentin request:
I have tried to create separate simple example to simulate the behavior and strange is that there is working:
<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>
See log:
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
I do not see a difference what has been done by other way. Except that the subfunction has been now called only with one parameter.