JS, function returns undefined instead of true or false

David Sarvasidze

I have a function which checks some parameters and returns either true/false accordingly.

My problem is that it returns true (the last return true;), but then when I call this function again and it gets to console.log(return 1) or console.log(return 2),the function only executes the console.log() and then returns undefined instead of true or false. My assumption is that its not allowed to return from .map() unless its finished running?

isFlashingUnderscore() {
  let count = 0;
  if (!_.isEmpty(this.currentElement.value)) {
    _.map(Object.keys(this.currentElement.value), key => {
      count++
      if (this.currentElement.value[key].object_type == 'date_range') {
        return false;
      } else if (this.currentElement.value[key].object_type == 'date') {
        if (count >= 2) {
          console.log('return 1');
          return false;
        } else {
          console.log('return 2');
          return true;
        }
      } else {
        return true;
      }
    })
  } else {
    console.log('returns this true')
    return true;
  }
}
jstuartmilne

So inside your if block if(!_.isEmpty(this.currentElement.value)){ you are returning nothing. you are just calling a map but not returning anything. Im not sure what you want to do with that map maybe check every value is true maybe check at least one is. not sure. the return within the map function is for the map lambda meaning you are returning something for each iteration within that map.

say you want to return true if every value is true, then something like this is what you want:

return  _.map(Object.keys(this.currentElement.value), key => {
          count++
          if (this.currentElement.value[key].object_type == 'date_range') {
            return false;
          } else if (this.currentElement.value[key].object_type == 'date') {
            if (count >= 2) {
              console.log('return 1');
              return false;
            } else {
              console.log('return 2');
              return true;
            }
          } else {
            return true;
          }
        }).every(a=>a);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Function returns undefined instead of true/false

Prime number function returns true instead of false

Javascript Function return true/false returns undefined

why "true || undefined ? undefined : false;" returns "undefinied"?

JS / JSX function always returns true even if it's false

Pipeline returns True / False instead of matching lines

Python Returns none instead of True or False

Should be returning true but returns false instead?

Undefined array.length returns true not false

AuthGuard can activate goes to end of function and returns false instead of waiting for subscription to return true

Function call returns Promise {_U: 0, _V: 0, _W: null, _X: null} instead of true or false

PySpark: StructField(..., ..., False) always returns `nullable=true` instead of `nullable=false`

Why String.Concat returns 'True' instead of 'true' (the same with false)?

Testing ReactJS component methods with enzyme shallow - function returns undefined instead of true

vb function returns true when false

isBefore() in moment js returning false instead of true

libreoffice calc if function returns true instead of value

false || false returns true

Parsing function only returns undefined instead of array

Jest mock function returns undefined instead of object

JS callback returns undefined is not a function

js function call returns undefined

isValid() function from moment.js library returns false for something that should be true

python 3.4 condition involving byte literal returns false instead of true

NSFileManager.defaultManager().fileExistsAtPath returns false instead of true

Laravel boolean returns "1"/"0" instead of true/false in query

C# Check if Regex is a match, returns True instead of False

It return false instead of true

JS Checking return function with if, always returns false