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;
}
}
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.
Comments