0

i have an array of values that i want to check for to see if it contains 3 specific values and if it does not, i would like to check which ones that the array does not contain.

3 values:

'docx', 'pdf', 'jpg'

array:

var comps = [
    { file: 'docx' },
    { file: 'pdf' },
    { file: 'txt' },
    { file: 'png' },
    { file: 'pdf' }
]

function:

function checkCodes () {
    angular.forEach( comps, function (comp) {
        if(comp.file === 'docx' && comp.file === 'pdf' && comp.file === 'jpg') {
            return true;
        } else {
            return false;
        }
    })
}

currently i think its looping over one at a time so it always returns false based on that only 1 value is being checked at a time.

2
  • 2
    comp.file can't possibly be equal to 'docx' and to 'pdf'. It could be one, or the other, or something else. Commented Apr 4, 2019 at 21:18
  • Your return false is inside forEach block. The very first time you find an element that does not match your condition it fires and you get your false result. Commented Apr 4, 2019 at 21:20

1 Answer 1

1

this is easier

hasAnyDoc = comps.findIndex( f => f.file === 'docx' || f.file === 'pdf' || f.file === 'jpg' ) > -1
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.