我如何遍历数组并获取所有项目的价值
How can I loop through array and get value of all it's items
我想遍历一个对象数组并获取所有对象的所有数据,但我只获取最后一个对象的数据我不明白为什么它会跳过第一个对象,请任何人帮助我
这是我的数组
item {
_id: new ObjectId("627ed71261b2bd692178bdca"),
sn: 'fN300W15076fd5bg6dwf',
merchantId: '9000000058800',
merchantName: '999',
networkType: '220509',
__v: 0,
terminalId: '987654321',
resultData: [
{
_id: new ObjectId("627edb194884d85b48890da8"),
uniqueID: '68461616865',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb494884d85b48890daa"),
uniqueID: '71616516',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb5c4884d85b48890dac"),
uniqueID: '7117117717',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'APPROVED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb674884d85b48890dae"),
uniqueID: '7117117717',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'PENDING',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
}
----------------------------------------------------
item {
_id: new ObjectId("627ed78561b2bd692178bdd3"),
sn: 'fN300W15076fd5bg6dwf',
merchantId: '9000000058800',
merchantName: '999',
networkType: '220509',
__v: 0,
terminalId: '123456789',
resultData: [
{
_id: new ObjectId("627edb884884d85b48890db0"),
uniqueID: '97279279',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'GOOD',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb994884d85b48890db2"),
uniqueID: '928498452',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edba54884d85b48890db4"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edbe0ade509d1bd9b5277"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
}
----------------------------------------------------
[
{
_id: new ObjectId("627edb884884d85b48890db0"),
uniqueID: '97279279',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'GOOD',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb994884d85b48890db2"),
uniqueID: '928498452',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edba54884d85b48890db4"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edbe0ade509d1bd9b5277"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
如你所见,我只得到最后一个对象的数组
对于那些好奇我的代码是什么样子的人
async getLogByTPE() {
let result;
const data = await this.tpeModel
.aggregate([
{
$lookup: {
from: 'logs',
localField: 'terminalId',
foreignField: 'terminalId',
as: 'resultData',
},
},
// { $unwind: '$resultData' },
// {
// $project: {
// uniqueID: '$logs.currenuniqueIDcyCode',
// terminalId: '$logs.terminalId',
// transactionAmount: '$logs.transactionAmount',
// currencyCode: '$logs.currencyCode',
// transactionDate: '$logs.transactionDate',
// transactionTime: '$logs.transactionTime',
// transactionType: '$logs.transactionType',
// cardPAN_PCI: '$logs.cardPAN_PCI',
// onlineRetrievalReferenceNumber:
// '$logs.onlineRetrievalReferenceNumber',
// outcome: '$logs.outcome',
// encryptionKeyKCV: '$logs.encryptionKeyKCV',
// transactionEncrypted: '$logs.transactionEncrypted',
// },
// },
])
.then((ok) => {
//start of test
// for (let i = 0; i <= ok.length; i++) {
// console.log('ok', ok[i].resultData[i]);
// data1 = JSON.stringify(ok[i]);
// }
//end of test
// console.log('OOOKKKK', ok);
ok.forEach((item) => {
console.log('item', item);
console.log('----------------------------------------------------');
//console.log('resultData', item.resultData);
result = item.resultData;
// result = item['resultData'].forEach((sub) => {
// console.log('sub', sub);
// return sub;
// });
});
});
// let subitemresult;
// let result = data.forEach((item) => {
// console.log('item', item);
// console.log(
// 'item.resultData',
// item.resultData.forEach((subitem) => {
// console.log('subitem', subitem);
// }),
// );
// });
// console.log('data', data);
// console.log('result', JSON.stringify(result));
console.log(result);
return result;
}
ok => 我的对象数组
item => 数组内的对象
resultData => 我要获取的数据
不幸的是我只有最后一个对象的结果数据如上所示
在循环中存储数据的结果变量在每次迭代时都会被覆盖,因此当您 return 时,只会存储最后一个值。
一个快速修复方法是在你的 result
声明中分配一个空数组
let result: any = [];
然后在循环内您可以推送到数组结果。
将此 result = item.resultData;
更改为
result.push(item.resultData);
我想遍历一个对象数组并获取所有对象的所有数据,但我只获取最后一个对象的数据我不明白为什么它会跳过第一个对象,请任何人帮助我
这是我的数组
item {
_id: new ObjectId("627ed71261b2bd692178bdca"),
sn: 'fN300W15076fd5bg6dwf',
merchantId: '9000000058800',
merchantName: '999',
networkType: '220509',
__v: 0,
terminalId: '987654321',
resultData: [
{
_id: new ObjectId("627edb194884d85b48890da8"),
uniqueID: '68461616865',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb494884d85b48890daa"),
uniqueID: '71616516',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb5c4884d85b48890dac"),
uniqueID: '7117117717',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'APPROVED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb674884d85b48890dae"),
uniqueID: '7117117717',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '987654321',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'PENDING',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
}
----------------------------------------------------
item {
_id: new ObjectId("627ed78561b2bd692178bdd3"),
sn: 'fN300W15076fd5bg6dwf',
merchantId: '9000000058800',
merchantName: '999',
networkType: '220509',
__v: 0,
terminalId: '123456789',
resultData: [
{
_id: new ObjectId("627edb884884d85b48890db0"),
uniqueID: '97279279',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'GOOD',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb994884d85b48890db2"),
uniqueID: '928498452',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edba54884d85b48890db4"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edbe0ade509d1bd9b5277"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
}
----------------------------------------------------
[
{
_id: new ObjectId("627edb884884d85b48890db0"),
uniqueID: '97279279',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'GOOD',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edb994884d85b48890db2"),
uniqueID: '928498452',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edba54884d85b48890db4"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
},
{
_id: new ObjectId("627edbe0ade509d1bd9b5277"),
uniqueID: '6824684294',
transactionAmount: '050413',
currencyCode: '9000000058800',
terminalId: '123456789',
transactionDate: '999',
transactionTime: '220509',
transactionType: 'DECLINED',
cardPAN_PCI: 'XXXXXXXXXXXXXXX166',
onlineRetrievalReferenceNumber: '6161651613516',
outcome: 'DONE',
encryptionKeyKCV: '651616161616516',
transactionEncrypted: 'SUHTHROTRTH161461'
}
]
如你所见,我只得到最后一个对象的数组
对于那些好奇我的代码是什么样子的人
async getLogByTPE() {
let result;
const data = await this.tpeModel
.aggregate([
{
$lookup: {
from: 'logs',
localField: 'terminalId',
foreignField: 'terminalId',
as: 'resultData',
},
},
// { $unwind: '$resultData' },
// {
// $project: {
// uniqueID: '$logs.currenuniqueIDcyCode',
// terminalId: '$logs.terminalId',
// transactionAmount: '$logs.transactionAmount',
// currencyCode: '$logs.currencyCode',
// transactionDate: '$logs.transactionDate',
// transactionTime: '$logs.transactionTime',
// transactionType: '$logs.transactionType',
// cardPAN_PCI: '$logs.cardPAN_PCI',
// onlineRetrievalReferenceNumber:
// '$logs.onlineRetrievalReferenceNumber',
// outcome: '$logs.outcome',
// encryptionKeyKCV: '$logs.encryptionKeyKCV',
// transactionEncrypted: '$logs.transactionEncrypted',
// },
// },
])
.then((ok) => {
//start of test
// for (let i = 0; i <= ok.length; i++) {
// console.log('ok', ok[i].resultData[i]);
// data1 = JSON.stringify(ok[i]);
// }
//end of test
// console.log('OOOKKKK', ok);
ok.forEach((item) => {
console.log('item', item);
console.log('----------------------------------------------------');
//console.log('resultData', item.resultData);
result = item.resultData;
// result = item['resultData'].forEach((sub) => {
// console.log('sub', sub);
// return sub;
// });
});
});
// let subitemresult;
// let result = data.forEach((item) => {
// console.log('item', item);
// console.log(
// 'item.resultData',
// item.resultData.forEach((subitem) => {
// console.log('subitem', subitem);
// }),
// );
// });
// console.log('data', data);
// console.log('result', JSON.stringify(result));
console.log(result);
return result;
}
ok => 我的对象数组
item => 数组内的对象
resultData => 我要获取的数据
不幸的是我只有最后一个对象的结果数据如上所示
在循环中存储数据的结果变量在每次迭代时都会被覆盖,因此当您 return 时,只会存储最后一个值。 一个快速修复方法是在你的 result
声明中分配一个空数组 let result: any = [];
然后在循环内您可以推送到数组结果。
将此 result = item.resultData;
更改为
result.push(item.resultData);