如何过滤json的重复数据?

时间:2017-10-10 19:08:31

标签: javascript jquery json

你可以在代码片段中看到并测试我有这个查询并带来getjson数据的函数,我需要的是不重复相同的数据。

如何过滤重复的结果,以免出现?



  var cuit = "30712413871";
  $.getJSON("https://soa.afip.gob.ar/av/v1/vencimientos/" + cuit, function(result) {
    for (var i = 0; i < result.data.length; i++) {
      var fecha = result.data[i].vencimiento;
      var periodo = fecha.substr(0, 7);
      console.log(result.data[i].idImpuesto);
      buscarChoice(result.data[i].idImpuesto, result.data[i].anticipoCuota, result.data[i].vencimiento, result.data[i].tipoOperacion, periodo);
    }
  });



function buscarChoice(num, op, venc, tipo, per) {
  $.getJSON("https://soa.afip.gob.ar/parametros/v1/impuestos/", function(result) {
    for (var i = 0; i < result.data.length; i++) {
      if (result.data[i].idImpuesto == num) {
        var table = document.getElementById("AFIP_edit");
        var row = table.insertRow(-1);

        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);


        cell1.innerHTML = result.data[i].idImpuesto;
        cell2.innerHTML = result.data[i].descImpuesto;
        cell3.innerHTML = '<label class="hidden">AFIP</label>';
      }
    }
  });
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<table id="AFIP_edit" class="table table-striped">
  <thead>
    <tr>
      <th class="text-center">#</th>
      <th>Impuesto</th>
      <th class="hidden">Agencia</th>
    </tr>
  </thead>
  <tbody class="tb">

  </tbody>
</table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

以下是你想要的吗?

var cuit = "30712413871";
var already_here = [];
  $.getJSON("https://soa.afip.gob.ar/av/v1/vencimientos/" + cuit, function(result) {
    for (var i = 0; i < result.data.length; i++) {
if(already_here.includes(result.data[i].idImpuesto)) {
continue;
}
      var fecha = result.data[i].vencimiento;
      var periodo = fecha.substr(0, 7);
      console.log(result.data[i].idImpuesto);
      buscarChoice(result.data[i].idImpuesto, result.data[i].anticipoCuota, result.data[i].vencimiento, result.data[i].tipoOperacion, periodo);
      already_here.push(result.data[i].idImpuesto);
    }
  });



function buscarChoice(num, op, venc, tipo, per) {
  $.getJSON("https://soa.afip.gob.ar/parametros/v1/impuestos/", function(result) {
    for (var i = 0; i < result.data.length; i++) {
      if (result.data[i].idImpuesto == num) {
        var table = document.getElementById("AFIP_edit");
        var row = table.insertRow(-1);

        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);


        cell1.innerHTML = result.data[i].idImpuesto;
        cell2.innerHTML = result.data[i].descImpuesto;
        cell3.innerHTML = '<label class="hidden">AFIP</label>';
      }
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<table id="AFIP_edit" class="table table-striped">
  <thead>
    <tr>
      <th class="text-center">#</th>
      <th>Impuesto</th>
      <th class="hidden">Agencia</th>
    </tr>
  </thead>
  <tbody class="tb">

  </tbody>
</table>

BTW,浏览器对我使用的.includes()方法的支持不是很好,所以我建议使用for循环遍历项目。

答案 1 :(得分:0)

这是使用array.filter的基本解决方案。如果您将支持旧版本的IE,则需要使用polyfil填充来使用该方法。

另外请注意,是否有/soa.afip.gob.ar/parametros/v1/impuestos/ api调用的版本可以返回您正在查找的一个ID的数据?如果没有,您应该调用该API一次并存储该列表。这样,您只能进行2次ajax调用,而不是数据集中每项1 + 1次。

  var cuit = "30712413871";
  $.getJSON("https://soa.afip.gob.ar/av/v1/vencimientos/" + cuit, function(result) {
    var ids = {};
    var noDups = result.data.filter(function(item) {
      if(ids[item.idImpuesto]){
         return false;
      }
      ids[item.idImpuesto] = true;
      return true;
    });
    for (var i = 0; i < noDups.length; i++) {
      var fecha = noDups[i].vencimiento;
      var periodo = fecha.substr(0, 7);
      console.log(noDups[i].idImpuesto);
      buscarChoice(noDups[i].idImpuesto, noDups[i].anticipoCuota, noDups[i].vencimiento, noDups[i].tipoOperacion, periodo);
    }
  });



function buscarChoice(num, op, venc, tipo, per) {
  $.getJSON("https://soa.afip.gob.ar/parametros/v1/impuestos/", function(result) { 
    for (var i = 0; i < result.data.length; i++) {
      if (result.data[i].idImpuesto == num) {
        var table = document.getElementById("AFIP_edit");
        var row = table.insertRow(-1);

        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);


        cell1.innerHTML = result.data[i].idImpuesto;
        cell2.innerHTML = result.data[i].descImpuesto;
        cell3.innerHTML = '<label class="hidden">AFIP</label>';
      }
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<table id="AFIP_edit" class="table table-striped">
  <thead>
    <tr>
      <th class="text-center">#</th>
      <th>Impuesto</th>
      <th class="hidden">Agencia</th>
    </tr>
  </thead>
  <tbody class="tb">

  </tbody>
</table>

答案 2 :(得分:0)

您可以使用//array with data var data = [ {"id" : 1, "value" : "a"}, {"id" : 2, "value" : "b"}, {"id" : 3, "value" : "b"}, {"id" : 4, "value" : "c"}, {"id" : 5, "value" : "d"}, {"id" : 6, "value" : "a"} ]; var map = []; result = data.filter(function(element){ if (map.indexOf(element.value) === -1) { map.push(element.value); return element; } }); console.log(result);。使用额外的数组来跟踪匹配,您可以过滤掉双值。请看下面的例子。

&#13;
&#13;
  var player = {};
  player.x = 0;
  player.y = 0;
  player.vx = 0;
  player.vy = 0;
  player.facing = "";
  player.jumping = false;
  player.shooting = false;
  player.animating = true;
  player.animationSpeed = 100;
  player.frame = 0;
  player.img = new Image();
  player.src = null;
  //I edited out the frames for length issues. They are player.frame0 through player.frame31
  player.physics = function() {
    document.onkeydown = function(e) {
      e = e || window.event;
      if (e.keyCode == 37) {
        player.vx -= player.vx;
        player.vx -= 3;
        player.facing = "left";
        player.animating = true;
      } else if (e.keyCode == 38) {
        player.vy -= player.vy;
        player.vy -= 10;
        player.jumping = true;
        player.animating = true;
      } else if (e.keyCode == 39) {
        player.vx -= player.vx;
        player.vx += 3;
        player.facing = "right";
        player.animating = true;
      } else if (e.keyCode == 32) {
        player.shooting = true;
        player.animating = true;
      }
    }
    document.onkeyup = function() {
      player.animating = false;
      player.jumping = false;
      player.vx = 0;
      player.vy = 0;
    }
    if (player.facing.toLowerCase() === "right" && player.animating === true) {
      setInterval(function() {
        player.src = player.frame0;
        setTimeout(function() {
          player.src = player.frame1;
          setTimeout(function() {
            player.src = player.frame2;
            setTimeout(function() {
              player.src = player.frame3;
            },player.animationSpeed);
          },player.animationSpeed);
        },player.animationSpeed);
      },player.animationSpeed);
    }
    drawImg(player,player.x,player.y);
  }
&#13;
&#13;
&#13;