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.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
jeremy
  • 433
  • 1
  • 8
  • 30
  • 2
    comp.file can't possibly be equal to 'docx' **and** to 'pdf'. It could be one, **or** the other, **or** something else. – JB Nizet Apr 04 '19 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. – PM 77-1 Apr 04 '19 at 21:20

1 Answers1

1

this is easier

hasAnyDoc = comps.findIndex( f => f.file === 'docx' || f.file === 'pdf' || f.file === 'jpg' ) > -1
Khaled Sameer
  • 296
  • 2
  • 10