0

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.

AlexZeDim
  • 2,626
  • 1
  • 20
  • 50
  • 1
    `exampleMap.get(string)`? – VLAZ Apr 27 '20 at 16:07
  • @VLAZ Oh sorry, I forgot to mention a detail. Not it's more relevant to my case. So `get` by key not an option in my case, cause `string` never exactly the same as a Map(key) – AlexZeDim Apr 27 '20 at 16:07
  • 2
    OK...you could do it as a one liner but I wouldn't call it elegant. You have to materialise the entire `entries` iterator, then filter, then work with the filtered results. The problem is that you're looking for an overlap, not an exact match. – VLAZ Apr 27 '20 at 16:10
  • @VLAZ, well, yeah somekind of. It should be like a `lookbehind` where I found does one of key values in `Map` have a match with `string` and then took a necessary `value` or it's `[index]` – AlexZeDim Apr 27 '20 at 16:12
  • 1
    exampleMap.forEach((value, key) => str.includes(key) ? console.log(str + value) : null) – Colin Apr 27 '20 at 16:19
  • @Colin, it's deserved to become an answer! Could you post it below, please? – AlexZeDim Apr 27 '20 at 16:24
  • 1
    @AlexZeDim exampleMap.forEach((value, key) => str.includes(key)&&console.log(str + value)) even shorter. inspired by palash's answer – Colin Apr 27 '20 at 16:35

3 Answers3

3

Use forEach to iterate map and check if key included in the string

let str = 'James never fly to Dubai'

let exampleMap = new Map([
    ['Sasha', ' goes to school'],
    ['James', ' goes to college'],
    ['Kevin', ' goes to work'],
]);

exampleMap.forEach((value, key) => str.includes(key)&&console.log(str + value))
Colin
  • 1,135
  • 7
  • 9
1

To get O(n) lookup time, .entries() and .includes won't help you. Instead you would've to iterate the string somehow, e.g. splitting by words (which seems to work in your case):

 string.split(" ").some(word => exampleMap.has(word))
Jonas Wilms
  • 120,546
  • 16
  • 121
  • 140
  • Yea, it's useful. But as I know `.has` returns `Boolean` true/false. So right after receiving `true` from it, I should also find an `[index] of match. (That isn't a problem for me) – AlexZeDim Apr 27 '20 at 16:16
  • 1
    You could `.find` and `.get` too. – Jonas Wilms Apr 27 '20 at 16:29
1

You can convert the exampleMap.entries() to an array and then apply array .some() method like this:

let string = 'James never fly to Dubai'

let exampleMap = new Map([
    ['Sasha', ' goes to school'],
    ['James', ' goes to college'],
    ['Kevin', ' goes to work'],
]);

[...exampleMap.entries()].some(([k,v]) => string.includes(k) && console.log(string, v))
palaѕн
  • 68,816
  • 17
  • 108
  • 129