从javascript函数返回数组不起作用

时间:2016-12-19 08:47:43

标签: javascript arrays

我有一个函数在子进程中处理一些数组。

目标是将实际数组发送到函数中并返回更新的数组(删除了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

我认为通过其他方式所做的事情没有区别。除了现在只用一个参数调用子函数。

2 个答案:

答案 0 :(得分:0)

报道的问题似乎不是问题。

我刚刚在代码中重新格式化了行,它开始起作用了。

可能隐藏的角色逃脱了真正的回报值。

答案 1 :(得分:0)

你不能因为$ .ajax()是(并且应该是)异步函数。

发生的事情是traceSystem()函数在实际调用成功回调之前返回

因此,在实际修改之前,您将返回 <bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> <property name="preAuthenticatedUserDetailsService"> <bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"> <property name="userDetailsService" ref="authenticationService"/> </bean> </property> </bean> 个对象。

服务器端你可以使用 deasync (nodejs模块),但在浏览器中我认为最好的办法就是重写代码以异步方式进行。

即,例如:

  1. masters函数添加回调参数。

  2. $。ajax()成功回调结束时调用它(将 masters 作为参数传递)。

  3. 而不是......

  4. 这样:

    traceSystem()

    ...试试:

    [...]
    resultMaster = traceSystem(arg1, arg2...);
    console.log('resultMaster: ' + resultMaster);
    [...]