Search code examples
javascriptloopsfor-of-loop

How to merge two for..of loops in JavaScript?


This is my loop containing the keys:

for(let keys of Object.keys(this.reportKeys[i].txnTypeKeys)){
 console.log("key: ",org.serviceCode+keys);
}

This is my loop containing objects:

for(let value of Object.values(this.activityReportData[0].services[i].txnTypes)){
 console.log("value:",value)
}

It give output as :

key:  DGEkey1
key:  DGEkey2
key:  DGEkey3
value: 19/06/2020 13:35:11
value: 19/06/2020 13:40:13
value: 49

key:  OLAkey1
key:  OLAkey2
key:  OLAkey3
value: 56
value: 41
value: 78

But I want something like -

key:  DGEkey1
value: 19/06/2020 13:35:11
key:  DGEkey2
value: 19/06/2020 13:40:13
key:  DGEkey3
value: 49

How is it possible?


Solution

  • You don't need to use Object.values for the second one. If you have keys that's enough to retrieve values.

    for(let key of Object.keys(this.reportKeys[i].txnTypeKeys)){
     console.log("key: ",org.serviceCode+keys);
     const value = this.activityReportData[0].services[i].txnTypes[key];
     console.log("value:",value)
    }