shios shios - 3 years ago 136
Javascript Question

how to compare or relate 2 getJson?

I need to compare these two Json's and get get1 to return all the data except the ones that the idConcepto and impuesto are the same as get2

example:


get1 =
https://maxtechglobal.com/vencimientos/agip/ib.php?cuit=30712413871


"data": [
{
"impuesto": "IVA",
"idConcepto": "30",
"id": "266",
"datos": {
"anticipo": "PRESENTACION",
"vencimiento": "2017-09-18",
"agencia": "AFIP",
"id_perfil": "146"
}
},
{
"impuesto": "GANANCIAS SOCIEDADES",
"idConcepto": "10",
"id": "268",
"datos": {
"anticipo": "PAGO",
"vencimiento": "2017-09-13",
"agencia": "AFIP",
"id_perfil": "146"
}
}
]



get2 =
http://estudiomiramonte.com/app/api/impuestos_modal.php?id_perfil=146


"data": [
{
"impuesto": "IVA",
"idConcepto": "30",
"id": "607",
"datos": {
"anticipo": "PRESENTACION",
"vencimiento": "2017-09-18",
"agencia": "AFIP",
"id_perfil": "187"
}
}
]



Result i need:


"impuesto": "GANANCIAS SOCIEDADES",
"idConcepto": "10",
"id": "268",
"datos": {
"anticipo": "PAGO",
"vencimiento": "2017-09-13",
"agencia": "AFIP",
"id_perfil": "146"




//get1

$.getJSON("https://maxtechglobal.com/vencimientos/agip/ib.php?cuit=" + "cuit", function(result) {
for (var i = 0; i < result.data.length; i++) {
if (result.data[i].id != otheresult.data[i].id) {
var table_abril = document.getElementById("AGIP_edit_todos");
var row = table_abril.insertRow(-1);

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

cell1.innerHTML = result.data[i].id;
cell2.innerHTML = result.data[i].impuesto;
cell3.innerHTML = '<input type="checkbox" name="optionsCheckboxes">';
}
}
});







//get2

$.getJSON("api/impuestos_modal.php?id_perfil=" + id + "", function(result) {});




Answer Source

You can use array#filter and array#some to get the data which don't share same idConcepto and impuesto.

var get1 = {"data": [{"impuesto": "IVA","idConcepto": "30","id": "266","datos": {"anticipo": "PRESENTACION","vencimiento": "2017-09-18","agencia": "AFIP","id_perfil": "146"}},{"impuesto": "GANANCIAS SOCIEDADES","idConcepto": "10","id": "268","datos": {"anticipo": "PAGO","vencimiento": "2017-09-13","agencia": "AFIP","id_perfil": "146"}}]},
    get2 = { "data": [{"impuesto": "IVA","idConcepto": "30","id": "607","datos": {"anticipo": "PRESENTACION","vencimiento": "2017-09-18","agencia": "AFIP","id_perfil": "187"}}] };

var result = get1.data.filter(function(o1){
  return !get2.data.some(function(o2){
    return o1.impuesto === o2.impuesto && o1.idConcepto === o2.idConcepto;
  });
});

console.log(result);

Since, both of your data are being fetched from external source, you can use Promise.all(). Once you have both the result you can use the above function.

Live Example

var get1 = $.getJSON("https://maxtechglobal.com/vencimientos/agip/ib.php?cuit=" + "30712413871").done();
var get2 = $.getJSON("https://estudiomiramonte.com/app/api/impuestos_modal.php?id_perfil=" + "146").done();

Promise.all([get1, get2]).then(function([obj1, obj2]){
var result = obj1.data.filter(function(o1) {
  return !obj2.data.some(function(o2) {
    return o1.impuesto === o2.impuesto && o1.id === o2.idConcepto;
  });
});

console.log(result);
})
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download