I've been struggling with this for over 3 days and still haven't found a solution. It feels like trying to find a needle in a haystack, but I'm determined to figure it out.
My goal is to search for a specific key in an array of objects and if it contains (a)
or (b)
, I want to set another key to true
.
Unfortunately, I seem to be making a mistake somewhere in my code, but I can't pinpoint where.
Here is the current state of my code:
let reg = /\((.+?)\)/;
let aggregatedArray: Array<any> = [];
const allWeeksDescription = this.selectedPlan.weeklyOffer;
console.log(allWeeksDescription);
let prop = 'description';
for (prop in allWeeksDescription) {
allWeeksDescription[prop].forEach(function(k:any) {
aggregatedArray.push(k.description);
console.log(aggregatedArray);
})
}
Despite receiving a successful log in the console, I also encounter this error:
ORIGINAL EXCEPTION: allWeeksDescription[prop].forEach is not a function
My main objective is to find the key ('description')
and if its value matches the RegEx, then set another key ('exists')
to true
. If there's no match, the value should remain as false
. The RegEx pattern is stored in this variable:
let reg = /\((.+?)\)/;
When I check the console, the output looks something like this:
{
monday: [
{
description: "(a)",
exists: false
},
...
]
tuesday: [
{
description: "test",
exists: false
},
...
]
}
To sum it up, I need to iterate through the description
keys, match them with the Regex
, and update the corresponding exists
key based on the match result. Your assistance with this problem would be greatly appreciated as I've spent too much time on it already.
Thank you in advance.