0

The following function does the following:

1) Check if the object has non-empty categories (keys with a certain name).

2) Check each of the objects inside those categories. If the object has the property web and the property is not empty or undefined return false.

3) The last bit does the same, but when the property web is in the object not in the categories.

hasNoCategories (object) {
  for (let key in object) {
    const value = object[key]
    const isCategory = this.categories.indexOf(key) > -1
    if (value.constructor === Array && isCategory && value.length > 0) {
      let result
      value.map(pano => {
        if (pano.web !== undefined && pano.web !== '') {
          result = false
        }
      })
      // STUCK HERE
      return result
    }
    if (key === 'web' && value !== '') {
      return false
    }
  }
  return true
},

However, when the code sometimes stops in // STUCK HERE and never makes it to return true at the end.

One workaround is this:

if (pano.web !== undefined && pano.web !== '') {
  result = false
} else {
  result = true
}

But I'm against having two return true, I feel it kinds of muddle the logic (I just want to have one return true, the one at the end.)

How to modify the code so the code doesn't get stuck in // STUCK HERE and continues until reaching return true?

EDIT:

Sample input:

{
  "livingroom": [],
  "diningroom": [],
  "kitchen": [],
  "bedroom": [],
  "study": [],
  "bathroom": [],
  "toilet": [],
  "garden": [],
  "garage": [],
  "outdoors": [],
  "other": [],
  "id": "ZI4hteKxgr",
  "name": "Cuiti",
  "description": "",
  "visible": true,
  "user": "",
  "floorplan": "",
  "shareCode": "http://vrviewer.istaging.co#!/854703",
  "date": "2016/5/13",
  "isDirty": false
}
alex
  • 7,111
  • 15
  • 50
  • 77
  • When you reach a return, the function stops. Read [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Description) and [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return#Interrupt_a_function) – zer00ne May 18 '16 at 02:06
  • In the first `if` statement, if `result` doesn't become `false` then it is returning `undefined` and the function ends like @zer00ne mentioned. – DonovanM May 18 '16 at 02:14
  • @DonovanM So how can I do it so the function resumes and not end when returning `undefined`? – alex May 18 '16 at 02:18
  • Also when you use `let` you are limiting the scope of `key` to only the `for` loop. If you hope to reach that return true with a `key`, try `var`. Read this [post](http://stackoverflow.com/a/11444416/2813224). – zer00ne May 18 '16 at 02:18
  • I believe that's why your *workaround* works, is because it's within scope of `key`. – zer00ne May 18 '16 at 02:25
  • @alex You could use a `for` loop instead of `map` and only `return false` if that condition is met. – DonovanM May 18 '16 at 02:29
  • @DonovanM Could you post an answer illustrating that? – alex May 18 '16 at 02:30
  • Sure thing. Writing it now. – DonovanM May 18 '16 at 02:33

1 Answers1

2

This is example of doing this using a for loop instead of map for the first if statement.

hasNoCategories (object) {
  for (let key in object) {
    const value = object[key]
    const isCategory = this.categories.indexOf(key) > -1
    if (value.constructor === Array && isCategory && value.length > 0) {
      for (let i = 0, len = value.length; i < len; i++) {
        let pano = value[i]
        if (pano.web !== undefined && pano.web !== '') {
          return false
        }
      }
    }
    if (key === 'web' && value !== '') {
      return false
    }
  }
  return true
}

This could probably be done in a cleaner way, though. I think for loops tend to make code look less clean.

DonovanM
  • 1,174
  • 1
  • 12
  • 17