shios
shios

Reputation: 67

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) {});

Upvotes: 1

Views: 96

Answers (2)

Hassan Imam
Hassan Imam

Reputation: 22564

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>

Upvotes: 1

Antonio Campos
Antonio Campos

Reputation: 106

You can store the data from getJson async with promises in ES6 or jQuery. Once you have both getJSONs in such vars you can do it by lodash.remove() and Array.some() like this:

var _ = require('lodash');

var data1 = [...JSON1];

var data2 = [...JSON2];

_.remove(data1,  obj1 => data2.some(obj2 =>
    ((obj1.impuesto === obj2.impuesto) &&
        (obj1.idConcepto === obj2.idConcepto))));

If you neither work with ES6 nor promises you can go this way:

var data = {};

$.getJSON("https://maxtechglobal.com/vencimientos/agip/ib.php?cuit=" + "30712413871", function(obj) {
    this.data.data1 = obj;
    clearData(this.data);
}.bind({data:data}));

$.getJSON("api/impuestos_modal.php?id_perfil=" + "146", function(obj) {
    this.data.data2 = obj;
    clearData(this.data);
}.bind({data:data}));

function clearData(data) {

    var data1 = data.data1;
    var data2 = data.data2;

    if (data1 && data2) {
        _.remove(data1,  function(obj1) { data2.some(function (obj2) {
            return ((obj1.impuesto === obj2.impuesto) &&
                (obj1.idConcepto === obj2.idConcepto))})});
        console.log(data1); // Your solution
    }

}

Upvotes: 1

Related Questions