2017-10-10 88 views
0

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

我该如何过滤重复的结果以免它们出现?

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>

回答

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遍历的项目进行迭代。

+0

是啊,感谢的人! – shios

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>

0

您可以使用Array.prototype.filter。使用一个额外的数组来跟踪匹配,您可以过滤出双值。看下面的例子。

//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);