I am dealing with an array of objects
that requires looping through to create a new object with different properties. The issue arises when attempting to access a property before it is actually declared.
Here is the original object:
let data = [
{
"name": "one",
"total": 12,
"fec": "001"
},
{
"name": "one",
"total": 1,
"fec": "002"
},
{
"name": "two",
"total": 5,
"fec": "001"
}
]
This is the approach taken:
let result;
data.forEach((item) => {
result = {
name: item.name,
result: data.find((item) => item.fec === '001') ?.total,
dto: this.result + 5
}
})
The dilemma at hand: finding a way to access the result
property from within the dto
property inside the forEach()
.