I have the following array of array of objects that is a result of a CloudWatch
query using AWS-SDK-V3 JS
:
const respCW = [
[
{ field: '@timestamp', value: '2022-10-25 15:30:36.685' },
{
field: '@message',
value: 'foo'
},
{
field: '@ptr',
value: 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
}
],
[
{ field: '@timestamp', value: '2022-10-25 15:04:56.155' },
{
field: '@message',
value: 'bar'
},
{
field: '@ptr',
value: 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
}
]
]
I would lie to convert that array to the following:
const desiredResp = [
{
timestamp: '2022-10-25 15:30:36.685',
message: 'foo'
},
{
timestamp: '2022-10-25 15:04:56.155',
message: 'bar'
}
]
I have created the following code:
if(respCW.length>0){
respCW.forEach( arrOfObje=> {
let logObj = {
timestamp:"",
message:"",
}
arrOfObje.forEach(obj => {
if(obj.field === '@timestamp' || obj.field === '@message'){
if(obj.field === '@timestamp'){
date = new Date(obj.value)
logObj.timestamp = date.getTime()
}
if(obj.field === '@message'){
logObj.message = obj.value
newResult.push(logObj)
}
}
})
})
}
The code is working but I would like to know if there is a way to redesign it using array map or reduce.