简体   繁体   中英

Handle return statement in a better way

That's my function:

 const checkifHaveBomb = (column, row) => {
            let isBomb = false
            activeBombContainer.forEach(element => {

                if (element.column === column && element.row === row) {
                    isBomb = true;
                    return;
                }
            })
            if (isBomb) return true;
            else
                return false;
        }

This was my 1st try:

 const checkifHaveBomb = (column, row) => {

            activeBombContainer.forEach(element => {
                if (element.column === column && element.row === row) {
                   
                    return true;
                }
            })
            
            return false;
        }

I was thinking that returns will end the function, but i didnt work. I suppose that the return statement is applied to forEach instead of checkifHaveBomb.

There is a better way to write this?

Problem

return ends the function, forEach just calls a function for each element

Solutions

Some

You can try using some and return true if you want to finish:

const checkifHaveBomb = (column, row) => {
  let isBomb = false
  activeBombContainer.some(element => {

    if (element.column === column && element.row === row) {
      isBomb = true;
      return true;
    }
  })
  if (isBomb) return true;
  else return false;
}

Or even shorter:

const checkifHaveBomb = (column, row) => {
  return activeBombContainer.some(element => {
    if (element.column === column && element.row === row) {
      isBomb = true;
      return true;
    }
  })
}

loop statement

You can also just use a normal for..of loop

const checkifHaveBomb = (column, row) => {
  for (const element of activeBombContainer) {
    if (element.column === column && element.row === row) {
      return true
    }
  }
  return false
}

You could use Array.prototype.some instead of forEach .

const checkifHaveBomb = (column, row)
=> activeBombContainer.some( element=>element.column === column && element.row === row);

As you surmised, the return value of Array.prototype/forEach 's argument function is discarded after each call.

You want to iterate through the activeBombContainer array until the condition returns true . Sounds like find function might suit your needs:

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

You should be able to write the function as follows

checkifHaveBomb = (column, row) => {
    return activeBombContainer.find(element => {
        return element.column === column && element.row === row
    })
}

Alternatively you could use the some function because it returns a boolean value.

checkifHaveBomb = (column, row) => {
    return activeBombContainer.some(element => {
        return element.column === column && element.row === row
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM