Is there any option to check is string value exists in JavaScript Map structure and if it does, take the necessary key value?
For example:
let string = 'James never fly to Dubai'
let exampleMap = = new Map([
['Sasha', ' goes to school'],
['James', ' goes to college'],
['Kevin', ' goes to work'],
]);
if (string.includes(exampleMap.entries())) { //we get index value of match
/* and (string) James + (exampleMap[1])goes to college*/
}
As for now, I'm using this code:
for (let [value, index] of exampleMap.entries()) {
if (string.includes(value)) {
console.log(string + index);
}
}
But according to this article and a relevant answer for Object there should be an elegant one liner but I can't figure it out.
UPDATED: I'd like to thank everyone who propose their idea below, I mark this question as answered, but let's be honest there are various ways to achieve necessary result. And my mark a «accepted as answer» is a bit subjective. So for all the Stackoverflow community, I believe that every proposed variant will be rewarded by their contribution.