0

The array contains Items made up of a string and some variables. If one of the variables is undefined I would like to filter the whole item out of the Array. Example below.

Full Array:

[ 'hello', 'hi undefined', 'good day' ]

Filtered Array:

[ 'hello', 'good day' ]

Array Code:

  const testArray = [
            'hello',
            'hi ' + variableX, //variableX is undefined
            'good day'];

1 Answer 1

1

You can use string#includes to check if a word exists in another word or sentence.

const arr = [ 'hello', 'hi undefined', 'good day' ],
      result = arr.filter(word => !word.includes('undefined'));
console.log(result);

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

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.