简体   繁体   English

jQuery 脚本:我填充了一个数组但无法从外部访问它

[英]jQuery Script: I Populate An Array But Can't Access It From Outside

I'm starting to fiddle with jquery and I'm going kind of crazy... I am making a little script to fetch football players from a fantasy football website.我开始摆弄 jquery 并且我有点疯狂...我正在制作一个小脚本来从一个梦幻足球网站上获取足球运动员。 I have the following html and JS to work with:我有以下 html 和 JS 可以使用:

  <table class="table table-striped">
  <thead>
    <tr>
      <th>Jugador</th>
      <th>Equipo</th>
      <th>Puntos</th>
    </tr>
  </thead>
  <tbody>
    <tr class="jugador">
      <td>Sergio-Ramos</td>
      <td>Real Madrid</td>
      <td></td>
    </tr>
    <tr class="jugador">
      <td>Messi</td>
      <td>F.C. Barcelona</td>
      <td></td>
    </tr>
    <tr class="jugador">
      <td>Morales</td>
      <td>Levante</td>
      <td></td>
    </tr>
    <tr class="jugador">
      <td>Bale</td>
      <td>Real Madrid</td>
      <td></td>
    </tr>
  </tbody>
</table>

And the following JS:以及以下JS:

<script>
  var puntos_jugador = [];

  $(".jugador").each(function(index) {

    var nombre = $(this).find("td").eq(0).text();

    puntos_jugador = puntosJugador(nombre);

    console.log(nombre);
    console.log(puntos_jugador);

    $(this).find("td").eq(2).text("Hola");
  });

  function puntosJugador(nombre) {
    var puntos = [];

    $.get('https://www.comuniazo.com/comunio/jugadores/' + nombre, function(response) {

      $(response).find('.tr-points, .tr-status').each(function(fila) {
        //var jornada = $(this).find("td").eq(0).text();
        var puntos_jornada = $(this).find(".bar").text();
        puntos.push(puntos_jornada);
        //console.log('Jornada ' + jornada + ' ' + puntos);
      });
    });

    return puntos;
  }
</script>

The thing is the console.log(puntos_jugador) does return an array filled with the information:问题是 console.log(puntos_jugador) 确实返回了一个充满信息的数组:

在此处输入图片说明

However I cannot access puntos_jugador[0] or try puntos_jugador.toString() .但是我无法访问 puntos_jugador[0] 或尝试 puntos_jugador.toString() 。

Could anyone tell me what am I doing wrong (maybe everything) or give me some orientation on how to fix this?谁能告诉我我做错了什么(也许是所有事情)或给我一些关于如何解决这个问题的方向?

Thanks on beforehand and sorry for my low JS level, I'm working on it.预先感谢并为我的低 JS 水平感到抱歉,我正在努力。

The problem in your code is that you are doing an asynchronous call with $.get and then you return the result directly without waiting for the response, which will always be the empty array define above.您的代码中的问题是您正在使用$.get进行异步调用,然后您直接返回结果而不等待响应,这将始终是上面定义的空数组。
You should wait for the response and then call a callback (or use promises)您应该等待响应,然后调用回调(或使用承诺)

Something like this:像这样的东西:

  var puntos_jugador = [];

  $(".jugador").each(function(index) {

    var nombre = $(this).find("td").eq(0).text();

    puntosJugador(nombre, function(result) {
        console.log('done'); 
        puntos_jugador = result;
        console.log(nombre);
        console.log(puntos_jugador);
    });

    $(this).find("td").eq(2).text("Hola");
  });

  // <---- HERE pass a callback and then call it when you have all results.
  function puntosJugador(nombre, cb) {
    var puntos = [];
    $.get('https://www.comuniazo.com/comunio/jugadores/' + nombre, function(response) {
       console.log('response ',response)
      $(response).find('.tr-points, .tr-status').each(function(fila) {
        //var jornada = $(this).find("td").eq(0).text();
        var puntos_jornada = $(this).find(".bar").text();
        puntos.push(puntos_jornada);
        //console.log('Jornada ' + jornada + ' ' + puntos);
        console.log('here', puntos);
      });
      cb(puntos);
    });
  } 

Refactor your method to this:将您的方法重构为:

function puntosJugador(nombre) {
    var puntos = [];

    $.get('https://www.comuniazo.com/comunio/jugadores/' + nombre, function(response) {

      $(response).find('.tr-points, .tr-status').each(function(fila) {
        //var jornada = $(this).find("td").eq(0).text();
        var puntos_jornada = $(this).find(".bar").text();
        puntos.push(puntos_jornada);
        //console.log('Jornada ' + jornada + ' ' + puntos);
      });
      return puntos;
    });


  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM