javascriptarraysobjectcomparisonbooleanquery

How can I go through two arrays and check if the information matches


I have two arrays of objects. One with a json variable and one with an information variable. The information variable is the one that should be equal to the json variable. This is because, information is data from the front and json is data from the database, so I must check that this information is totally the same. In this case the example that I put in all are the same. But it is just for example. So what should I do:

  1. Compare the information variable with the json variable.
  2. Start comparing data by data.
  3. If the data is the same a message 'Everything went well' and if not 'there is an error'

I was trying it like this so that it goes through all the data but it throws me certain errors, such as the same element going through me twice. I was reading that it can also be done with filter, I have seen a lot of documentation but I cannot implement it in my case. Below in the code I show with whom to compare

 var json = [
{
  Transacción: "999999",
  Tarjeta: "0190",
  Tipo: "Contr ctdo",
  FechaDePago: "07/08/2022",
  Ventas: "-5.000,00",
  },
  {
   Transacción: "999997",
   Tarjeta: "0194",
    Tipo: "Contr ctdo",
   FechaDePago: "06/08/2022",
    Ventas: "4.000,00",
   },
   {
    Transacción: "999998",
     Tarjeta: "0195",
      Tipo: "Contr ctdo",
     "FechaDePago": "08/08/2022",
      Ventas: "6.000,00",
    },
   ];


var informacion = [
{
Trx: "Contr ctdo",
Fecha: "07/08/2022",
TermLoteCupon: "999999",
Tarj: "0190",
VentasconDto: "-5.000,00",
},
{
Trx: "Contr ctdo",
Fecha: "06/08/2022",
TermLoteCupon: "999997",
Tarj: "0194",
VentasconDto: "4.000,00",
},
{
Trx: "Contr ctdo",
Fecha: "08/08/2022",
TermLoteCupon: "999998",
Tarj: "0195",
VentasconDto: "6.000,00",
},
];

// The comparison should be that the data object array must be equal to the
  array of json objects.
 //The comparison is :
  //Trx must be equal to Tipo 
  //Fecha must be equal to FechaDePago
   //TermLoteCupon must be equal to Transacción
  // Tarj must be equal to Tarjeta
  //VentasconDto must be equal to Ventas
  // What I did was the following: 
 for (let i = 0; i < informacion.length; i++) {
     console.log('soy newarray', informacion[i]);
      for(let j = 0; j < json.length; j++){
        console.log('soy json parseado',json[j]);
         if(json[j] == informacion[i]){
           console.log('La informacion es compatible')
         }else{
           console.log('Hay error.')
          }
       }

       }

enter image description here


Solution

  • The OP just ...

    1. needs to implement a comparison function which returns a boolean value in case both passed transaction items (e.g. an item's key-value pair from the example's informacion array has to match its counterpart item of different key-value pairs from the example's json array) ...
    2. ... and then needs to iterate the informacion array by Array.prototype.every while passing each currently processed informacion item together with its json counterpart-item where the latter is determined by the current idx-value.

    As soon as the every based comparison process hits a mismatch, the iteration stops and returns the boolean false value whereas an uninterrupted full cycle means that all the requirements/conditions have been met, thus every returning the boolean true value.

    const json = [{
      Transacción: "999999",
      Tarjeta: "0190",
      Tipo: "Contr ctdo",
      FechaDePago: "07/08/2022",
      Ventas: "-5.000,00",
    }, {
      Transacción: "999997",
      Tarjeta: "0194",
      Tipo: "Contr ctdo",
      FechaDePago: "06/08/2022",
      Ventas: "4.000,00",
    }, {
      Transacción: "999998",
      Tarjeta: "0195",
      Tipo: "Contr ctdo",
      "FechaDePago": "08/08/2022",
      Ventas: "6.000,00",
    }];
    
    const informacion = [{
      Trx: "Contr ctdo",
      Fecha: "07/08/2022",
      TermLoteCupon: "999999",
      Tarj: "0190",
      VentasconDto: "-5.000,00",
    }, {
      Trx: "Contr ctdo",
      Fecha: "06/08/2022",
      TermLoteCupon: "999997",
      Tarj: "0194",
      VentasconDto: "4.000,00",
    }, {
      Trx: "Contr ctdo",
      Fecha: "08/08/2022",
      TermLoteCupon: "999998",
      Tarj: "0195",
      VentasconDto: "6.000,00",
    }];
    
    function hasMatchingTransactionValues(sample, proof) {
      return (
        sample.TermLoteCupon === proof['Transacción'] &&
        sample.VentasconDto === proof.Ventas &&
        sample.Fecha === proof.FechaDePago &&
        sample.Tarj === proof.Tarjeta &&
        sample.Trx === proof.Tipo
      );
    }
    function isMatchingTransactionData(sample, proof) {
      return (
        sample.length === proof.length &&
        sample
          .every((item, idx) =>
            hasMatchingTransactionValues(item, proof[idx])
          )
      );
    }
    console.log(
      'Does every transaction item match its counterpart ?..',
      isMatchingTransactionData(informacion, json),
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }