While I have come across several questions on this particular topic , none of them have been able to provide me with a solution.
Let's take a look at the data I am working with:
const testsMap = {
0: ["just", "test"],
1: ["bla", "asdf"]
}
const testArray = [{
id: "1",
segments: null,
tests: [{
id: "1",
segments: "1"
},
{
id: "2",
segments: "0"
}
]
},
{
id: "2",
segments: "1",
tutorials: [{
id: "1",
segments: "1"
},
{
id: "2",
segments: "0"
}
]
}];
My objective here is to achieve the desired output without utilizing .map()
or .reduce
methods, as I intend to overwrite the existing array. Here's what I'm aiming for:
[{
display: true,
id: "1",
segments: null,
tests: [{
display: true,
id: "1",
segments: "1",
newSegments: ["bla", "asdf"]
},
{
display: true,
id: "2",
segments: "0",
newSegments: ["just", "test"]
}
]
},
{
display: false,
id: "2",
segments: "1",
tutorials: [{
id: "1",
segments: "1"
},
{
id: "2",
segments: "2"
}
]
}];
The function I have written appears like this - it includes some helper functions which can be disregarded, as the main focus is on the fact that the function returns undefined
:
function SOtest () {
const returnedValue = testArray.forEach(test => {
test.newSegments = test.segments ? testsMap[test.segments] : [];
test.display = helperFn(); // will assign true/false to the test property
if (test.display) {
test.tests.map(t => {
t.newSegments = t.segments ? testsMap[t.segments] : [];
t.display = helperFn(); // will assign true/false to the test property
})
}
return test;
})
return returnedValue;
}
The issue I am facing is that the forEach
loop operates correctly when executed in isolation in the console, but once I try to return it, it results in undefined
.
What could I possibly be overlooking here?