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:
- Compare the information variable with the json variable.
- Start comparing data by data.
- 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.')
}
}
}
CodePudding user response:
The OP just ...
- 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'sjson
array) ... - ... and then needs to iterate the
informacion
array byArray.prototype.every
while passing each currently processedinformacion
item together with itsjson
counterpart-item where the latter is determined by the currentidx
-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; }
CodePudding user response:
Yes you can do it with filter:
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",
},
];
const result = json.filter((item, i) =>
(item.Tipo === informacion[i].Trx)
&& (item.FechaDePago === informacion[i].Fecha)
&& (item.Transacción === informacion[i].TermLoteCupon)
&& (item.Tarjeta === informacion[i].Tarj)
&& (item.Ventas === informacion[i].VentasconDto)
)
console.log(result)
if(result.length === json.length){
console.log("it's equal");
}else{
console.log("it's not equal");
}
CodePudding user response:
You need to provide a helper function which will compare these two particular objects - since the objects hold same values but the actual naming of the keys is different.
function isEqual(json, informaction) {
return json.Transacción === informacion.TermLoteCupon &&
json.Tarjeta === informacion.Tarj &&
json.Tipo === informacion.Trx &&
json.FechaDePago === informacion.Fecha &&
json.Ventas === informacion.VentasconDto;
}
Then you use this helper function within your comparison instead of performing just a simple json[j] == informacion[i]
comparison. This comparison is not possible with objects.
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(isEqual(json[j], informacion[i])) {
console.log('La informacion es compatible')
} else {
console.log('Hay error.')
}
}
}
If the arrays are sorted in order (e.g. informacion[0].id === json[0].id
) and you want to avoid comparing each value of informacion
to each value of json
you can do the following:
for(let i = 0; i < informacion.length; i ) {
if(isEqual(json[i], informacion[i])) {
console.log('La informacion es compatible')
} else {
console.log('Hay error.')
}
}
if they are not, then you will have to figure out a way to loop through and compare them which suits your use-case.