javascript - 如何过滤json的重复数据?

标签 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>';
      }
    }
  });
}
<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>

最佳答案

以下是您想要的吗?

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>

顺便说一句,浏览器对我使用的 .includes() 方法的支持不是很好,因此我建议使用 for 循环来迭代项目。

关于javascript - 如何过滤json的重复数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46674185/

相关文章:

自动完成的 Jquery 样式

javascript - 使用 Closest 属性 Jquery 获取 Div 元素内输入标签的计数

jquery - jquery ajax实际是如何工作的

java - 在jackson中以ISO8601格式反序列化 "Zulu"时间

json - 尼菲 : Extract Content of FlowFile and Add that Content to the Attributes

javascript - 按下按钮时删除特定 cookie

javascript - 如何将变量从一个页面传递到另一页面?

javascript - 如何返回axios外的变量?

javascript - 添加表单标签时出现未定义错误

java - Gson:直接将String转换为JsonObject(无POJO)