I am currently working on developing a feature to detect and flag inappropriate comments within my application.
One approach I am taking involves splitting the comment into an array of strings. I am now looking to implement a JavaScript function that can assign a numerical value to each array item based on predefined key-value pairs stored in a JSON object. If a matching key is not found, the item should be replaced with a 0.
The ultimate goal is to sum up all the numerical values in the final array to calculate a comment score.
For instance, given the initial array:
["Bad", "reallyBad", "Good", "Neutral", "Good"]
I plan to compare it against a JSON object with key-value pairs like:
{
"reallyBad": -10,
"Bad": -5,
"Good": 5,
"reallyGood": 10
}
Following these mappings, the updated array should look like this:
[-5, -10, 5, 0, 5]
If you have any insights on how to efficiently convert strings based on key-value pairs, I would greatly appreciate your guidance.
Any assistance you can provide would be immensely valuable.