我有兩個物件陣列。一個帶有 json 變數,一個帶有資訊變數。資訊變數應該等于 json 變數。這是因為,資訊是來自前面的資料,而json是來自資料庫的資料,所以我必須檢查這些資訊是否完全相同。在這種情況下,我放入的所有示例都是相同的。但這只是舉例。所以我該怎么做:
- 將資訊變數與 json 變數進行比較。
- 開始逐個資料比較。
- 如果資料相同,則訊息“一切順利”,否則“出現錯誤”
我是這樣嘗試的,以便它遍歷所有資料,但它會給我帶來某些錯誤,例如同一個元素通過我兩次。我正在閱讀它也可以使用過濾器來完成,我已經看過很多檔案,但我無法在我的情況下實作它。在下面的代碼中,我展示了與誰進行比較
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.')
}
}
}

uj5u.com熱心網友回復:
OP只是...
- 需要實作一個比較函式,該函式回傳一個布林值,以防兩個傳遞的事務項(例如,示例陣列中的項的鍵值對
informacion必須匹配示例陣列中不同鍵值對的對應項json)... - ...然后需要迭代
informacion陣列,Array.prototype.every同時將每個當前處理的informacion專案與其json對應項一起傳遞,其中后者由當前idx值確定。
一旦every基于比較程序遇到不匹配,迭代就會停止并回傳布爾false值,而不間斷的完整回圈意味著所有要求/條件都已滿足,因此every回傳布爾true值。
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; }
uj5u.com熱心網友回復:
是的,你可以用過濾器做到這一點:
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");
}
uj5u.com熱心網友回復:
您需要提供一個輔助函式來比較這兩個特定物件 - 因為物件具有相同的值,但鍵的實際命名不同。
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;
}
然后在比較中使用這個輔助函式,而不是僅僅執行一個簡單的json[j] == informacion[i]比較。物件無法進行這種比較。
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.')
}
}
}
如果陣列按順序排序(例如informacion[0].id === json[0].id),并且您希望避免將 的每個值與informacion的每個值進行比較,json則可以執行以下操作:
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.')
}
}
如果不是,那么您將不得不找出一種方法來回圈并比較適合您的用例的它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526600.html
