0

i'm trying to check two values and if they are met do something if not obviously throw an error. Now the problem im having is that the array needs to be completely checked before the error has to be thrown. EX:

  • in my sheet i have a letter d
  • in my array CODELIJST i have the following values - [a,b,c,d,e,f,g]
  • when we loop the first time the else statement is active because d != a -> how can i loop trough whole array and if the value is not there throw an error?
    (let u=1; u < CODELIJST.length; ++u){
     if (CODELIJST[u][0]==shift) {
      var testvar2 = CODELIJST[u][1];
     }else{
     // throw error if value is not found
     }
    }

The Array is two dimensional thats why im only looping through with var u

2 Answers 2

2

You can use a else if and break:

if (CODELIJST[u][0] === shift) {
  var testvar2 = CODELIJST[u][1];
  break;
} else if (u === CODELIJST.length) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the o so simple solution, didn't think so far as when counter is at max you can throw error. Thanks again !
0

EDITED: As recommended by TheMaster, using array.forEach instead of array.map since this doesn't have a return value. This checks each array element if it's a string or not without using a loop.

Try this:

function checkArrayValType() {

  var d, f = 2; 
  var array = ['a','b','c',d,'e',f];

  array.forEach(val = index => (typeof index === "string") ? console.log("String") : console.log("not string"))

}

This is a sample implementation to check if the values within an array is a string or not using array.map function.

References:

enter image description here

2 Comments

If you're not going to use the return array, it's better to use array.forEach() instead. See Anti pattern.
Thanks for the clarification! I'll edit my answer as recommended

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.