1

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;
  }
}
3
  • 7
    The return statements inside the _.map() call return only from the callback function, not the outer containing function. Commented Nov 20, 2019 at 15:33
  • Hi Pointy, thank you for your quick reply! Correct me if I am wrong please, so there is no way to directly return true or false from _.map() ? I would have to set a variable to true or false and then return it from the outer function? Commented Nov 20, 2019 at 15:36
  • 1
    @DavidSarvasidze why do you need to use _.map? You're not actually doing a mapping operation you seem to be using it for simple iteration which is not its purpose. What's the goal here? Commented Nov 20, 2019 at 15:37

3 Answers 3

2

Not an answer to your actual problem but your code can be simplified. After a return, the remainder of the function does not get executed. You can make use of that:

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;
            }
            if(this.currentElement.value[key].object_type == 'date'){
                return false;
            }
            if (count >= 2) {
                return false;
            }
        })
    }
    return true;
}

By simplifying it, it becomes a lot easier to understand what is going on and what it is you don't want to happen.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0
please Try this 

isFlashingUnderscore(){
    let count = 0;
    if(!_.isEmpty(this.currentElement.value)){
      var returnVal = _.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;
        }
      })
     if(returnVal){
           return true;
        }else{
          return false;
        }
      }else{
       console.log('returns this true')
       return true;
     }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.