-2

Downvoting is okay, but at least let me know why you considered to do so.

I have learning passion to see if there is alternative solution, and that's why I asked this question with pre-research.

I'm refactoring some existing codes that "storing data at runtime" from "Object" to "Map"(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/).

The goal is processing the given runtime data without returning the results.

Since filter and map can be applied to Object.keys, it's clear for developers to understand what's the logic inside the process.

Old code sample,

Object.keys(givenObj)
    .filter(key => { return typeof givenObj[key] === 'string' && givenObj[key] !== undefined })
    .map( key => { processData(givenObj[key]) } )

It's obvious for people understanding what should be "filtered" before doing the next.

However, with "Map", the validation of value seems can only be done through either forEach or values in a new iteration.

For example,

givenMap.forEach( (value, key) => { 
    if ( typeof value !== 'string' || value === undefined ){ 
       invalidHandling(key)
    } else {
       processData(value)
    } 
})

Question:

Whether if is the only way to handle the undefined value in "Map"?

Mark
  • 169
  • 1
  • 8
  • With arrays, there are often *many* ways you can handle iteration/filtering/validation/etc... but you shouldn't `.map` to "`Do some works`", you should `.map` to *create a new array* only. I'm not sure what exactly you're asking – CertainPerformance Aug 06 '18 at 02:48
  • do you need to return a value? then use map or filter, else if you only need to iterate over an array use forEach – mpm Aug 06 '18 at 02:51
  • @CertainPerformance What you are pointing out is the code before I took over the project, and this question is specified to ask about "Map" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/ – Mark Aug 06 '18 at 02:57
  • What exactly are you trying to do? Can you post some of the actual code, the expected input and output? It's not very clear – CertainPerformance Aug 06 '18 at 02:59
  • @CertainPerformance I believe the latest edited question is now clear to understand – Mark Aug 06 '18 at 03:13
  • @mpm No need to return the result, but I'm trying to avoid using `if else` – Mark Aug 06 '18 at 03:14
  • @Kaiido You are right, I made a mistake in previous edit, and now it's corrected – Mark Aug 06 '18 at 03:18
  • And what is so wrong with this `if` that you want to get rid of it? Maybe it would be cleaner to move it at the time you do generate this Map, or inside the processData itself, but anyway, you seem to need it, and even though it was inside the internal `filter` function in the ES5 version, it was still there. – Kaiido Aug 06 '18 at 03:20
  • @Kaiido Actually I did the refactor using `if` with "Map" implementation, but some of our Sr. Engineers consider `if else` is a bad practice. Thus, I'm asking on SO to see if there is alternative solution. – Mark Aug 06 '18 at 03:25
  • if else a bad practice? Is single branch programming still programming or just declaration? – Kaiido Aug 06 '18 at 03:27
  • @Kaiido I see your point, I assume the arguments from other engineers are standing on DX, since `filter` and `map` are more specified terms to be understood. I sincerely suggest you post an answer to explain why `if else` is still required in my case, and simply describe `if else` is the internal of `filter`. My question is "Whether if is the only way to handle the undefined value in "Map"?" and I believe you have answered it. – Mark Aug 06 '18 at 03:37

1 Answers1

1

What you're asking is "Do Maps/iterators support declarative programming?"

Currently, there is no native support for what you want. You can see one of a few discussion about their inclusion here.


The four options I see people take are:

a. Just use an if else statement

Not a bad choice - but some people just feel like it disrupts their flow.

b. Convert to an array, then back.

This is obviously not the most performant or clean solution.

c. Add your own filter and map methods

Either on the Map or on the iterator, and either actually extending the object (generally discouraged), or a function with takes it as an argument. Maybe check out this stack overflow answer or look for some libraries for some ideas (maybe wu.js could help?).

d. Don't use Maps

Not really an "option" per say but its definitely the conclusion some come to so I though I'd list it as they just feel like they've got a flow they're used to that Maps don't support (yet).

SamVK
  • 3,077
  • 1
  • 14
  • 19
  • Although in this particular example, it looks like `.forEach` should have been used over `.map` regardless. – SamVK Aug 06 '18 at 04:36
  • Thanks for the explanation and case-specified suggestion. The question title has been modified for better relevant search – Mark Aug 06 '18 at 04:55